How To Escape Semicolons When Writing To Csv In Python
I have a list of lists in python that represents data to be written in a csv file. My code for that is : for n, d, p in zip(names, dates, posts): writer.writerow([i, n, d,
Solution 1:
I will recommend using pandas for converting data into CSV as it's much easier and you don't need to care about handling characters.
>>>arr = [{"a":1,"b":2,"c":3},{"a":2,"b":3,"c":4}]>>>dataFrame = pd.DataFrame(arr)>>>dataFrame
a b c
0 1 2 3
1 2 3 4
>>>dataFrame.to_csv("test.csv",index = 0)
Solution 2:
You can use tab as a delimiter.
You can write the file and open later using pandas or csv, setting delimiter as “\t”
.
It works for opening in Excel or similar programs too.
Example using csv for writing:
import csv
withopen(<filename>, "w") as file:
writer = csv.writer(file, delimiter="\t")
Example using csv for reading:
import csv
withopen(<filename>, "r") as file:
reader = csv.reader(file, delimiter="\t")
Solution 3:
It could be an issue with whatever program you're viewing the CSV with. I had the same problem and it was because I had a checkbox selected in my spreadsheet software to delimit by both commas and semicolons.
Post a Comment for "How To Escape Semicolons When Writing To Csv In Python"