2

你好,我有这段文字:

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

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

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

4

2 回答 2

4

不。无需删除换行符。

使用csv模块读取 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'

为那一行。

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

于 2013-10-27T13:25:35.207 回答
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']

'字符串'值是:

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
于 2013-10-27T13:41:16.880 回答