Python 如果re.match返回null,我想处理一个异常
我已经写了一小段代码: 我想让它做的是: 添加异常处理,据我所知,如果方法Python 如果re.match返回null,我想处理一个异常,python,python-2.7,Python,Python 2.7,我已经写了一小段代码: 我想让它做的是: 添加异常处理,据我所知,如果方法match() 匹配文件中的任何内容都会返回值None,但是我不太明白 如何读取该值进行比较并打印脚本(版本不匹配) 有人有什么建议吗?如果有一些指向web文档的链接也不错 提前谢谢你 你走对了路。由于None的布尔值为False,您只需在代码中使用else分支: if re.match('(.*)4.30.1(.*)', line): print 'The version match: '+
match()
匹配文件中的任何内容都会返回值None
,但是我不太明白
如何读取该值进行比较并打印脚本(版本不匹配)
有人有什么建议吗?如果有一些指向web文档的链接也不错
提前谢谢你 你走对了路。由于None
的布尔值为False,您只需在代码中使用else
分支:
if re.match('(.*)4.30.1(.*)', line):
print 'The version match: '+ line
else:
print 'incorrect version'
现在我很确定您想要匹配文件的第一行(包含版本号的那一行)或整个文件,以防万一:
#first line
with open(x) as f:
try:
#next(f) returns the first line of f, you have to handle the exception in case of empty file
if re.match('(.*)4.30.1(.*)', next(f))):
print 'The version match: '+ line
else:
print 'incorrect version'
except StopIteration:
print 'File %s is empty' % s
#anything
with open(x) as f:
if re.match('(.*)4.30.1(.*)', f.read())):
print 'The version match: '+ line
else:
print 'incorrect version'
你走对了。由于None
的布尔值为False,您只需在代码中使用else
分支:
if re.match('(.*)4.30.1(.*)', line):
print 'The version match: '+ line
else:
print 'incorrect version'
现在我很确定您想要匹配文件的第一行(包含版本号的那一行)或整个文件,以防万一:
#first line
with open(x) as f:
try:
#next(f) returns the first line of f, you have to handle the exception in case of empty file
if re.match('(.*)4.30.1(.*)', next(f))):
print 'The version match: '+ line
else:
print 'incorrect version'
except StopIteration:
print 'File %s is empty' % s
#anything
with open(x) as f:
if re.match('(.*)4.30.1(.*)', f.read())):
print 'The version match: '+ line
else:
print 'incorrect version'
因为re.match()
如果找到匹配项,则返回true
。因此,如果no
找到匹配项,则只需使用else语句
import csv
import re #### don't need it
import os #### don't need it
fileobj = csv.reader(open('c:\\paths1.csv', 'rb'), delimiter=' ', quotechar='|')
for row in fileobj:
for x in row:
with open(x) as f:
for line in f:
if '4.30.1' in line: #### much simpler than regex
print 'The version match: '+ line
break
else: # Yes, a `for` statement can have an `else:`
# end of file, "break" doesn't arrive here
print 'incorrect version' # done ONCE at end of file
因为re.match()
如果找到匹配项,将返回true
。因此,如果没有找到匹配项,只需使用else语句即可。
非常感谢!!!!我也尝试过类似的东西,但我不认为它可以这么简单:)我喜欢python(我以前用turbo pascal编程)-1。如果没有一行匹配,OP希望打印“不正确的版本”。此代码将在每一个不匹配的行中打印一次。@JohnMachin是的,他还说,match
在与文件中的任何内容不匹配时返回none,然后再重新匹配行,所以OP真正想要的内容至少不明显。@soulcheck:你真的认为他想打印“不正确的版本”吗对于每个不匹配的行?@soulcheck:或者版本可以位于文件中的任何行上(请参阅我的答案)。不管怎样,我已经取消了否决票。非常感谢!!!!我也尝试过类似的东西,但我不认为它可以这么简单:)我喜欢python(我以前用turbo pascal编程)-1。如果没有一行匹配,OP希望打印“不正确的版本”。此代码将在每一个不匹配的行中打印一次。@JohnMachin是的,他还说,match
在与文件中的任何内容不匹配时返回none,然后再重新匹配行,所以OP真正想要的内容至少不明显。@soulcheck:你真的认为他想打印“不正确的版本”吗对于每个不匹配的行?@soulcheck:或者版本可以位于文件中的任何行上(请参阅我的答案)。不管怎么说,我已经取消了否决票。如果没有一行匹配,OP希望打印“不正确的版本”。此代码将按不匹配的行打印一次。-1。如果没有一行匹配,OP希望打印“不正确的版本”。此代码将按不匹配的行打印一次。
import csv
import re #### don't need it
import os #### don't need it
fileobj = csv.reader(open('c:\\paths1.csv', 'rb'), delimiter=' ', quotechar='|')
for row in fileobj:
for x in row:
with open(x) as f:
for line in f:
if '4.30.1' in line: #### much simpler than regex
print 'The version match: '+ line
break
else: # Yes, a `for` statement can have an `else:`
# end of file, "break" doesn't arrive here
print 'incorrect version' # done ONCE at end of file