Python CSV模块 - 引号丢失 [英] Python CSV module - quotes go missing

查看:466
本文介绍了Python CSV模块 - 引号丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个CSV档案,其资料类似这样

  15,I,2,41301888,BYRNESS RAW ,,BYRNESS VILLAGE,NORTHUMBERLAND,ENG
11,I,3,41350101,2,2935,2,2008-01-09,1,8,0,2003 -02-01,,2009-12-22,2003-02-11,377016.00,601912.00,377105.00,602354.00,10

我正在读这个,然后将不同的行写入不同的CSV文件。



但是,在原始数据中, ,因为其中一些在字段中包含逗号。



我无法保留引号。



我已经研究了很多,发现 quoting = csv.QUOTE_NONNUMERIC 然而,这现在导致在每个字段的引号,我不知道为什么



如果我尝试其他引用选项,如MINIMAL,我最终得到一个关于日期值,2008-01-09,不是一个浮点的错误消息。



我试图创建一个方言,在csv读写器和编写器上添加引用,但我没有尝试得到与原始数据完全匹配的结果。


$

解决方案

写作时, quoting = csv.QUOTE_NONNUMERIC 保持值不加引号,只要它们是数字,即。如果它们的类型是 int float (例如),这意味着它会写你期望的。 >

您的问题可能是,在阅读时, csv.reader 会将每行读取为 list (如果您仔细阅读文档,则会看到一个读取器不会执行自动数据类型转换! / p>

如果您在阅读 后不执行任何类型的转换,那么当您写入时,因为你写的每一个都是一个字符串。



编辑:当然,日期字段将被引用,因为它们不是数字,使用标准 csv.writer 获得确切的预期行为。


I have a CSV file that has data like this

15,"I",2,41301888,"BYRNESS RAW","","BYRNESS VILLAGE","NORTHUMBERLAND","ENG"
11,"I",3,41350101,2,2935,2,2008-01-09,1,8,0,2003-02-01,,2009-12-22,2003-02-11,377016.00,601912.00,377105.00,602354.00,10

I am reading this and then writing different rows to different CSV files.

However, in the original data there are quotes around the non-numeric fields, as some of them contain commas within the field.

I am not able to keep the quotes.

I have researched lots and discovered the quoting=csv.QUOTE_NONNUMERIC however this now results in a quote mark around every field and I dont know why??

If i try one of the other quoting options like MINIMAL I end up with an error message regarding the date value, 2008-01-09, not being a float.

I have tried to create a dialect, add the quoting on the csv reader and writer but nothing I have tried results in the getting an exact match to the original data.

Anyone had this same problem and found a solution.

解决方案

When writing, quoting=csv.QUOTE_NONNUMERIC keeps values unquoted as long as they're numbers, ie. if their type is int or float (for example), which means it will write what you expect.

Your problem could be that, when reading, a csv.reader will turn every row it reads into a list of strings (if you read the documentation carefully enough, you'll see a reader does not perform automatic data type conversion!

If you don't perform any kind of conversion after reading, then when you write you'll end up with everything on quotes... because everything you write is a string.

Edit: of course, date fields will be quoted, because they are not numbers, meaning you cannot get the exact expected behaviour using the standard csv.writer.

这篇关于Python CSV模块 - 引号丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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