Convert string representation of list to list with quote and double quotes
- Use the
converters
parameter ofpandas.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
How to Write Python Array (Data = []) to Excel
Removing White Space Around a Saved Image
Converting Json into Newline Delimited Json in Python
Python/Regex - How to Extract Date from Filename Using Regular Expression
Populating a List in Python Using for Loop
Sum Numbers of Each Row of a Matrix Python
How to Convert a Pandas Dataframe to a Pytorch Tensor
How to Stop Execution of All Cells in Jupyter Notebook
Grab a Number After a String in a File
Winerror 10049: the Requested Address Is Not Valid in Its Context
Pandas - How to Compare 2 CSV Files and Output Changes
Python - How to Separate Paragraphs from Text
Discord.Py Show Who Invited a User
Python: Getting Around Division by Zero
Python: Split a List into Multiple Lists Based on a Subset of Elements
How to Resolve Modulenotfounderror: No Module Named 'Google.Colab'