Python 从csv到csv输出的正则表达式结果
我想将结果保存在我的csv中。但不知何故,它仍然是空的。但是,如果我不在csv中写入相同的输出,而只是Python 从csv到csv输出的正则表达式结果,python,regex,python-2.7,python-3.x,csv,Python,Regex,Python 2.7,Python 3.x,Csv,我想将结果保存在我的csv中。但不知何故,它仍然是空的。但是,如果我不在csv中写入相同的输出,而只是打印,我会得到我想要的结果 import re import glob import os ## is the second script for matchParser.py, match the results for Duration in ms digit lines = [line.strip() for line in open('output.csv')] for result i
打印
,我会得到我想要的结果
import re
import glob
import os
## is the second script for matchParser.py, match the results for Duration in ms digit
lines = [line.strip() for line in open('output.csv')]
for result in lines:
match = re.search(r'(!?\s.\d[Request completed in\s])', result)
if match: print match.group(0)
但是这个版本的输出是不工作的,我不知道为什么
import re
import glob
import os
lines = [line.strip() for line in open('output.csv')]
for result in lines:
match = re.search(r'(!?\s.\d[Request completed in\s])', result)
with open('outputREGEX5.csv', "w") as output_csv:
if match: output_csv.write(match.group(0))
我还得到以下错误:
output.write(match.group(0))
AttributeError: 'NoneType' object has no attribute 'group'
我感谢你的帮助
--
编辑
我的打印
输出:
44
37
53
35
17
35
25
27
50
31
27
36
17
66
46
41
38
23
33
清空文件。只在循环外打开文件一次,而不是每次迭代都打开它
with open('outputREGEX5.csv', "w") as output_csv:
for result in lines:
match = re.search(r'(!?\s.\d[Request completed in\s])', result)
if match: output_csv.write(match.group(0) + '\n')
清空文件。只在循环外打开文件一次,而不是每次迭代都打开它
with open('outputREGEX5.csv', "w") as output_csv:
for result in lines:
match = re.search(r'(!?\s.\d[Request completed in\s])', result)
if match: output_csv.write(match.group(0) + '\n')
你能不能解释得更准确一点,确切地说我应该把它放在哪里?我是pythonAlmost的新手。。。
match=…
行应该在if match:
之前,并且处于相同的缩进级别。它正在工作!但这些条目并不像印刷版那样采用新行。我怎样才能添加一个\n以便每次迭代都写入一行?@harun实际上就是在您正在编写的东西上添加一个\n。来吧,试试看,我试过了。不幸的是没有成功。总是会出现这样的错误:NameError:name'match'未定义:/能否请您更精确地解释一下,并确切地说我应该把它放在哪里?我是pythonAlmost的新手。。。match=…
行应该在if match:
之前,并且处于相同的缩进级别。它正在工作!但这些条目并不像印刷版那样采用新行。我怎样才能添加一个\n以便每次迭代都写入一行?@harun实际上就是在您正在编写的东西上添加一个\n。来吧,试试看,我试过了。不幸的是没有成功。始终获取此错误:名称错误:未定义名称“匹配”:/