I’m using the csv module in python and escape characters keep messing up my csv’s. For example, if I had the following:
import csv
rowWriter = csv.writer(open('bike.csv', 'w'), delimiter = ",")
text1 = "I like to \n ride my bike"
text2 = "pumpkin sauce"
rowWriter.writerow([text1, text2])
rowWriter.writerow(['chicken','wings'])
I would like my csv to look like:
I like to \n ride my bike,pumpkin sauce
chicken,wings
But instead it turns out as
I like to
ride my bike,pumpkin sauce
chicken,wings
I’ve tried combinations of quoting, doublequote, escapechar and other parameters of the csv module, but I can’t seem to make it work. Does anyone know whats up with this?
*Note – I’m also using codecs encode(“utf-8”), so text1 really looks like "I like to \n ride my bike".encode("utf-8")
The problem is not with writing them to the file. The problem is that
\nis a line break when inside''or"". What you really want is either'I like to \\n ride my bike'orr'I like to \n ride my bike'(notice therprefix).