Python 使用正则表达式从CSV中删除行内的换行符
你好,我有这段文字:Python 使用正则表达式从CSV中删除行内的换行符,python,regex,csv,Python,Regex,Csv,你好,我有这段文字: 1,0.00,,2.00,10,"Block. CertNot Valid. Query with me",2013-06-20,0,0.00 这是CSV文件中的两行,但实际上是一行数据,我想删除中断行,并使用正则表达式将这一行放在一行中 我尝试过:(\”)(.*)(\n)(.*)(\”),但它不起作用。不要。无需删除换行符 使用读取CSV文件,它将正确处理换行符: import csv with open(csvfilename, 'rb') as infile:
1,0.00,,2.00,10,"Block. CertNot Valid.
Query with me",2013-06-20,0,0.00
这是CSV文件中的两行,但实际上是一行数据,我想删除中断行,并使用正则表达式将这一行放在一行中
我尝试过:
(\”)(.*)(\n)(.*)(\”)
,但它不起作用。不要。无需删除换行符
使用读取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'
为了那一排
这是因为该列被正确引用。不要这样做。无需删除换行符 使用读取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'
为了那一排
这是因为该列被正确引用。您可以在此处检查结果: 控制台输出:
>>> 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
您可以在此处检查结果: 控制台输出:
>>> 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
为什么不直接使用
csv
模块来阅读?它将正确处理换行符。为什么不使用csv
模块来读取此内容?它会正确处理断线。