CSV删除字段值换行引号 [英] CSV remove field value wrap quotes

查看:152
本文介绍了CSV删除字段值换行引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写一个列表到csv,但是当我这样做,我得到包装报价围绕我的字段值:

  number1,number2 
1234,2345
1235.7890
2345.5687

使用这个代码:

  with open('C:\\temp\\\ \\ test.csv','wb')作为out_file:
... csv_writer = csv.writer(out_file,delimiter =',')
... csv_writer.writerow(('number1' 'number2'))
... for f in myList:
... csv_writer.writerow(f)

进一步研究后,我发现您可以使用以下内容删除引号的写法:


quotechar = '',quoting = csv.QUOTE_NONE **


当我将此应用于我的代码时,我收到此错误:


跟踪(最近一次调用):文件,行
4,在错误:需要转义,但没有escapechar集

'



 打开('C:\\temp\\ test.csv' wb')as out_file:
... csv_writer = csv.writer(out_file,delimiter =',',quotechar ='',quoting = csv.QUOTE_NONE)
csv_writer.writerow(('number1' ,'number2'))
... for f in myList:
... csv_writer.writerow(f)

如何删除这些引号?



编辑



myList如下:

  [['1234,2345'], '1235,7890'],['2345,5687']] 


解决方案

您的列表中的内容不是数字,而是文本,甚至包含分隔符。这意味着导出为csv它必须被转义。



您需要将您的数据转换为数字,然后导出到csv如果你想它被正确写入。



编辑:另一方面,你的数据已经看起来像是逗号分隔的值 - 为什么不使用csv writer直接写入文件? p>

I'm attempting to write a list to a csv, however when I do so I get wrapper quotes around my field values:

number1,number2
"1234,2345"
"1235.7890"
"2345.5687"

Using this code:

with open('C:\\temp\\test.csv', 'wb') as out_file:
...     csv_writer = csv.writer(out_file, delimiter=',')
...     csv_writer.writerow(('number1','number2'))
...     for f in myList:
...         csv_writer.writerow(f)

After further research, I found that you can remove the writing of quotes by using:

quotechar='', quoting=csv.QUOTE_NONE**

When I apply this to my code I get this error:

Traceback (most recent call last): File "", line 4, in Error: need to escape, but no escapechar set

with open('C:\\temp\\test.csv', 'wb') as out_file:
...     csv_writer = csv.writer(out_file, delimiter=',',quotechar='', quoting=csv.QUOTE_NONE)
        csv_writer.writerow(('number1','number2'))
...     for f in myList:
...         csv_writer.writerow(f)

How do I remove these quotes?

Edit

myList looks like:

     [['1234,2345'], ['1235,7890'], ['2345,5687']]

解决方案

what's in your list are not numbers but text, which is even containing the delimiter character. that means to export this as csv it has to be escaped.

you need to convert your data to numbers before you export it to csv if you want it to be written correctly.

edit: on the other hand your data already looks like it consits of comma separated values - why not write it to a file directly without using a csv writer?

这篇关于CSV删除字段值换行引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆