Avoiding quotes around row of string data with csv.writer
I am trying to write a row of strings (it's the header row) to a csv file using csv.writer as follows:
mbfile=open(pathtodata + "mbsensors.csv", "ab")
writer=csv.writer(mbfile)
writer.writerow(["ESReadings"])
i=1
tempstring='DateTime'
while i<=numsensors:
tempstring=tempstring+','+ "%02d"%i +'.Cooked'
i=i+1
print tempstring
writer.writerow([tempstring])
mbfile.close
(I don't know why that first row has indented - it's not like that in my code).
Using Python 2.7.3 in Linux.
writer.writerow(["ESReadings"]) writes fine, without surrounding double quotes.
print tempstring displays on screen as expected.
writer.writerow([tempstring]) writes fine, except it has double quotes around it.
I am importing this data into a Windows program and it won't like the quotes, how can I avoid them?
The csv writer thinks that you're trying to write a single entry, which is why it's putting quotes around the full string. You should pass the row as a list (or some other iterable) of strings, and let the writer insert the commas. Something like this:
tempstring=['DateTime']
while i<=numsensors:
tempstring.append("%02d"%i +'.Cooked')
i=i+1
print tempstring
writer.writerow(tempstring)
链接地址: http://www.djcxy.com/p/55090.html
上一篇: python csv复制列