How to Convert List into String With Quotes in Python

Convert string representation of list to list with quote and double quotes

  • Use the converters parameter of pandas.read_csv when reading the file in.
import pandas as pd
from ast import literal_eval

# test dataframe
ls = [['abc'],['a"bcd"e', "ab'cde'"]]
df = pd.DataFrame({'test': ls})

# save to csv
df.to_csv('test2.csv', index=False)

# read file in with converters
df2 = pd.read_csv('test2.csv', converters={'test': literal_eval})

print(type(df2.iloc[0, 0]))
[out]: list

How to convert string elements with single quotes to double quotes in a Python list

I'm pretty sure there is no way to force python to write strings with double quotes; the default is single quotes. As @deadshot commented, you can either replace the ' with " after you write the whole string to the file, or manually add the double quotes when you write each word. The answer of this post has many different ways to do it, the simplest being f'"{your_string_here}"'. You would need to write each string separately though, as writing a list automatically adds ' around every item, and that would be very spaghetti.

Just do find and replace ' with " after you write the string to the file.

You can even do it with python:

# after the string is written in 'data.txt'
with open('data.txt', "r") as f:
text = f.read()

text = text.replace("'", '"')

with open('data.txt', "w") as f:
text = f.write(text)


Edit according to OP's comment below

Do this instead of the above; this should fix most of the problems, as it searches for the string ', ' which, hopefully, only appears at the end of one string and the start of the next

with open('data.txt', "r") as f:
text = f.read()

# replace ' at the start of the list
text = text.replace("['", '["')

# replace ' at the end of the list
text = text.replace("']", '"]')

# replace ' at the item changes inside the list
text = text.replace("', '", '", "')

with open('data.txt', "w") as f:
text = f.write(text)


(Edit by OP) New edit based on my latest comment

Running this solves the problem I described in the comment and returns the expected solution.

with open('data.txt', "r") as f:
text = f.read()

# replace ' at the start of the list
text = text.replace("['", '["')

# replace ' at the end of the list
text = text.replace("']", '"]')

# replace ' at the item changes inside the list
text = text.replace("', '", '", "')

text = text.replace("', ", '", ')

text = text.replace(", '", ', "')

with open('data.txt', "w") as f:
text = f.write(text)


Related Topics



Leave a reply



Submit