使用正则表达式从CSV中删除行内的换行符

时间:2013-10-27 13:22:58

标签: python regex csv

您好,我有这样的文字:

1,0.00,,2.00,10,"Block. CertNot Valid.
Query with me",2013-06-20,0,0.00

这是CSV文件中的两行,但实际上是一行数据,我想删除断行,并使用正则表达式将此行放在一行中。

我尝试过:(\")(.*)(\n)(.*)(\"),但它不起作用。

2 个答案:

答案 0 :(得分:4)

别。 无需可以删除换行符。

使用csv module阅读CSV文件,它会正确处理换行符:

import csv

with open(csvfilename, 'rb') as infile:
    reader = csv.reader(infile)
    for row in reader:
        print repr(row[5])

将打印:

'Block. CertNot Valid.\nQuery with me'

该行。

这是有效的,因为该列被正确引用。

答案 1 :(得分:0)

您可以在此处查看结果:https://www.debuggex.com/r/2_X5N-wTLZ2laJKh

控制台输出:

>>> regex = re.compile("\"(.+?)\"",re.MULTILINE|re.DOTALL|re.VERBOSE)
>>> regex.findall(string)
[u'Block. CertNot Valid.\nQuery with me', u'test\naaa', u'bbb\nvvvv']

'string'值为:

1,0.00,,2.00,10,"Block. CertNot Valid.
Query with me",2013-06-20,0,0.00

1,0.00,,2.00,10,"test
aaa",2013-06-20,0,0.00

1,0.00,,2.00,10,"bbb
vvvv",2013-06-20,0,0.00