Write row to a CSV file without it adding quotatio

2020-07-22 17:38发布

My first problem:

writer.writerow(['Name,' 'Street,,' 'Address,,,'])

The above example returns "Name,Street,,Address,,,"

I need it to return Name,Street,,Address,,, without the quotation marks.

What should I do?

标签: python
3条回答
放荡不羁爱自由
2楼-- · 2020-07-22 17:57

Stop putting commas in your values.

writer.writerow(['Name', '', 'Street', '', '', 'Address', '', '', ''])
查看更多
疯言疯语
3楼-- · 2020-07-22 17:57
import csv
with open("lame.csv","w") as f:
      w = csv.writer(f)
      w.writerow(["frank","tom","mike","paul"])

does not put quotes in the file....

If you are seeing a string in python they always have quotes in the repr ... you can use print to not show the quotes that wrap a python string ...

查看更多
甜甜的少女心
4楼-- · 2020-07-22 18:04

Ignacio's answer is absolutely right and is the one you want to use in practice.

But if you want to instruct csv to never use quotes, which is what the question seems to be literally asking, you can set the dialect paramater for quoting to csv.QUOTE_none. It would look like:

    theWriter = csv.writer(open('thefile.csv', 'wb'), 
delimeter = ',', quoting = csv.QUOTE_NONE)

Also, if you really want to micromanage what goes intot he file, you can skip the csv library and just write strings to the file.

查看更多
登录 后发表回答