Home > OS >  replace single quotes with backticks
replace single quotes with backticks

Time:11-26

I need to enclose every list element with backticks `` so that the code can be sent to the frontend. Basically, I need to replace single quotes with backticks. Below is the list in which I need to make the change.

['<table border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td>
          <link
            href="https://www.moneycontrol.com/rss/latestnews.xml"
            rel="alternate"
            title="MoneyControl.com News"
            type="application/rss xml"
          />
        </td>
      </tr>
    </table>',
    '<table cellpadding="0" cellspacing="0" class="rhsglTbl" width="100%">
      <thead>
        <tr>
          <th width="120">Index</th>
          <th width="80">Price</th>
          <th width="55">Change</th>
          <th width="50">% Chg</th>
        </tr>
      </thead>
    </table>']

This change is required so that I can iterate through each element and add it to an accordian.

CodePudding user response:

like this?

backticked = [ f"`{x}`" for x in your_list ]

edit with extra info

this returns a list, just like the original, but with an starting and ending backtick to each element, so if you write it to a file to pass it to the frontend, it will have the backticks

in your python editor it would be

['`<table ... </table>`', '`<table ... `']

but when you write it to a file for your frontend it would be

`<table>...</table>`
`<table>...</table>`

which is what you need for JS substitution, no?

It also depends on how you pass the data, you might just do the template literal in the frontend as:

tableElement.innerHTML = `${ varFromPython }`
  • Related