title: How to convert string elements with single quotes to double quotes in a Python list
body: <p>I am preprocessing data for an NLP task and need to structure the data in the following way:</p>
  <blockquote>
  <p>[tokenized_sentence] tab [tags_corresponding_to_tokens]</p>
  </blockquote>
  <p>I have a text file with thousands of lines in this format, where the two lists are separated by a tab. Here is an example</p>
  <pre><code>['EU', 'rejects', 'German', 'call', 'to', 'boycott', 'British', 'lamb', '.']    ['I-ORG', 'O', 'I-MISC', 'O', 'O', 'O', 'I-MISC', 'O', 'O']
  </code></pre>
  <p>and the piece of code I used to get this is</p>
  <pre><code>with open('data.txt', 'w') as foo:
      for i,j in zip(range(len(text)),range(len(tags))):
          foo.write(str([item for item in text[i].split()]) + '\t' + str([tag for tag in tags[j]]) + '\n')
  </code></pre>
  <p>where <strong>text</strong> is a list containing sentences (i.e. each sentence is a string) and <strong>tags</strong> is a list of tags (i.e. the tags corresponding to each word/token in a sentence is a list).</p>
  <p>I need to get the string elements in the lists to have double quotes instead of single quotes while maintaining this structure. The expected output should look like this</p>
  <pre><code>[&quot;EU&quot;, &quot;rejects&quot;, &quot;German&quot;, &quot;call&quot;, &quot;to&quot;, &quot;boycott&quot;, &quot;British&quot;, &quot;lamb&quot;, &quot;.&quot;]    [&quot;I-ORG&quot;,  &quot;O&quot;, &quot;I-MISC&quot;, &quot;O&quot;, &quot;O&quot;, &quot;O&quot;, &quot;I-MISC&quot;, &quot;O&quot;, &quot;O&quot;]
  </code></pre>
  <p>I've tried using <code>json.dump()</code> and <code>json.dumps()</code> from the <code>json</code> module in Python but I didn't get the expected output as required. Instead, I get the two lists as strings. My best effort was to manually add the double quotes like this (for the tags)</p>
  <pre><code>for i in range(len(tags)):
      for token in tags[i]:
          tkn = &quot;\&quot;%s\&quot;&quot; %token
          print(tkn)
  </code></pre>
  <p>which gives the output</p>
  <pre><code>&quot;I-ORG&quot;
  &quot;O&quot;
  &quot;I-MISC&quot;
  &quot;O&quot;
  &quot;O&quot;
  &quot;O&quot;
  &quot;I-MISC&quot;
  &quot;O&quot;
  &quot;O&quot;
  &quot;I-PER&quot;
  &quot;I-PER&quot;
  .
  .
  .
  </code></pre>
  <p>however, this seems too inefficient. I have seen these related questions</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/1906991/convert-single-quoted-string-to-double-quoted-string">Convert single-quoted string to double-quoted string</a></li>
  <li><a href="https://stackoverflow.com/questions/39482614/converting-a-text-file-to-json-format-using-python">Converting a Text file to JSON format using Python</a></li>
  </ul>
  <p>but they didn't address this directly.</p>
  <p>I'm using Python 3.8</p>