CSV文件中DoubleDouble引号中的逗号 [英] Comma in DoubleDouble Quotes in CSV File

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

问题描述

我有一个如示例中给出的字符串:

I have a string like given in the example:

data = '02 JAN 2014,FEB 2014,A,1.00,,,""1,235.100000"",""1,230.00"",Column'

如何使用python的csv模块解析此字符串?

how can I parse this string with using python's csv module?

data = StringIO.StringIO(data) 
reader = csv.reader(data, quoting=csv.QUOTE_NONE)

它将字符串""1,235.10000""分为两列:'""1''235.1000""'

It separates the the string ""1,235.10000"" to two column: '""1' and '235.1000""'

如何解决此问题,并在双引号中将模块排列成不分割逗号?

how can I fix this and arrange the module to not to split commas if its in double-double quotes?

推荐答案

我不确定这是否足够好,但是:

I'm not sure if this is good enough, but:

>>> import csv
>>> data = '02 JAN 2014,FEB 2014,A,1.00,,,""1,235.100000"",""1,230.00"",Column'
>>> reader = csv.reader([data.replace('""', '|')], quotechar='|')
>>> next(reader)
['02 JAN 2014', 'FEB 2014', 'A', '1.00', '', '', '1,235.100000', '1,230.00', 'Column']

您可以使用StringIO或其他任何东西,但是传递列表可以简化示例代码:).如果实际上有一个文件对象,甚至可以使用一个简单的生成器来转换这些行,然后再将它们提供给阅读器:

You can keep with the StringIO or whatever, but passing in a list made the example code simpler :). If you actually have a file object, you could even just use a simple generator to transform the lines before you feed them to your reader:

def transform(file):
   for line in file:
       yield line.replace('""', '|')

with open('foo') as fin:
    reader = csv.reader(transform(fin), quotechar='|')
    ...

transform可以变得任意复杂-例如如果出于某种原因需要保留引号.

And transform can become as sophisticated as you like -- e.g. if you need to preserve the quotes for some reason.

这篇关于CSV文件中DoubleDouble引号中的逗号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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