I’m running this code below and outputting the results into a csv file:
df['Post Town'] = '"' + df['Post Town'].astype(str) + '"' df.to_csv('filename.csv', index=False)
However I’ve noticed that in notepad++ my strings are coming back with triple quotes. Is there a way around this as I only want ASCII double quotes?
to_csv() inserts the needed double quotes around your field already, but as the field contains double quotes (you insert them manually), those need to be escaped.
The CSV format is described in RFC-4180, which states “If double-quotes are used to enclose fields, then a double-quote appearing inside a field must be escaped by preceding it with another double quote.”
So, ‘your’ double quotes get escaped with double double quotes, and then another pair of double quotes is put around the field by
to_csv(). And since you put ‘your’ double quotes at start and end of the field, you’ll end up with triple double quotes.
- If you want the CSV reading process produce a string with single double quotes around it: The triple double quotes are correct.
- If you want the CSV reading process produce a string without quotes around it: let
to_csv()handle the outer quotes around the field.
- If you need a different variant of the CSV format (there are a lot of options), you need to edit the options of