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 th开发者_C百科is?
*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 \n
is a line break when inside ''
or ""
. What you really want is either 'I like to \\n ride my bike'
or r'I like to \n ride my bike'
(notice the r
prefix).
Firstly, it is not obvious why you want r"\n"
(two bytes) to appear in your file instead of "\n"
(one byte). What is the consumer of the output file meant to do? Use ast.evaluate_literal()
on each input field? If your actual data contains any of (non-ASCII characters, apostrophes, quotes), then I'd be very wary of serialising it using repr()
.
Secondly, you have misreported either your code or your output (or both). The code that you show actually produces:
"I like to
ride my bike",pumpkin sauce
chicken,wings
Thirdly, about your "I like to \n ride my bike".encode("utf-8")
: str_object.encode("utf-8")
is absolutely pointless if str_object
contains only ASCII bytes -- it does nothing. Otherwise it raises an exception.
Fourthly, this comment:
I don't need to call encode anymore, now that I'm using the raw string. There are a lot of unicode characters in the text that I am using, so before I started using the raw string I was using encode so that csv could read the unicode text
doesn't make any sense -- as I've said, "ascii string".encode('utf8')
is pointless.
Consider taking a step ot two backwards, and explain what you are really trying to do: where does your data come from, what's in it, and most importantly, what does the process that is going to read the file going to do?
精彩评论