我正试图从“糟糕”的数字中挑选出“好数字”。
我的问题是我从文本文件中获得的一些数字包含空格(" "
)。这些函数通过拆分空格来识别它们,这样所有包含空格的行都显示为坏数字,无论它们是好还是坏。
任何人都知道如何解决这些问题?我现在正在使用它。
def showGoodNumbers():
print ("all good numbers:")
textfile = open("textfile.txt", "r")
for line in textfile.readlines():
split_line = line.split(' ')
if len(split_line) == 1:
print(split_line) # this will print as a tuple
textfile.close
def showBadNumbers():
print ("all bad numbers:")
textfile = open("textfile.txt", "r")
for line in textfile.readlines():
split_line = line.split(' ')
if len(split_line) > 1:
print(split_line) # this will print as a tuple
textfile.close
文本文件如下所示(所有带注释的条目都是“坏”):
13513 51235
235235-23523
2352352-23
- 无效235235
- 太短了324-134 3141
23452566246
- 太长了答案 0 :(得分:5)
这是(另一个)Python re模块真正发挥作用的经典示例:
from re import match
with open("textfile.txt", "r") as f:
for line in f:
if match("^[0-9- ]*$", line):
print "Good Line:", line
else:
print "Bad Line:", line
<强>输出:强>
Good Line: 13513 51235
Good Line: 235235-23523
Bad Line: 2352352-23 - not valid
Bad Line: 235235 - too short
Good Line: 324-134 3141
Bad Line: 23452566246 - too long
答案 1 :(得分:1)
这里只需要字符串操作。
allowed_chars = ['-', '.', ' ', '\n']
with open("textfile.txt", "r") as fp:
for line in fp:
line_check = line
for chars in allowed_chars:
line_check = line_check.replace(chars, '')
if line_check.isdigit():
print "Good line:", line
else:
print "Bad line:", line
您可以在allowed_chars列表中添加任意数量的字符。只是为了方便添加字符。我在allowed_chars列表中添加了\ n,以便根据注释处理尾随的换行符。