我正在编制一个程序,用于排除有效和无效的社会安全号码。
该程序应该能够从我的计算机上的文本文件中对数字进行排序。但是我只能一次输入所有数字(我认为)。我不会一个接一个地检查数字的程序。
这就是它现在的样子
def fileinput():
try:
textfile = open("numberlist.txt","r")
socialsecuritynumber = textfile.read()
numberprogram(socialsecuritynumber)
except IOError:
print("there's no such file!\n")
任何人都知道我应该怎么做? 文本文件只包含数字
这是我的文本文件中的数字
答案 0 :(得分:4)
然后使用for循环逐行读取
with open("numberlist.txt","r") as textfile:
for line in textfile:
print line
答案 1 :(得分:0)
与建议的使用方式一起使用。您可以使用readLines()方法并使用for-in循环逐行迭代,并检查有效性。这将确保即使对于大型文件,您的代码也不会中断。
答案 2 :(得分:0)
with open("numberlist.txt") as f: # this auto closes the file after reading. It's good practice
numbers = f.readlines() # numbers is a list of all the numbers(a list of lines in the file)
如果行中有不需要的空格(或者只是存在):
numbers = [n.strip() for n in numbers] # takes out unwanted spaces on the ends
如果你发现数字后面有逗号或其他内容,你可以这样做:
numbers = [n[:-1] for n in numbers] # slices off the last character of each line/list item
for number in numbers:
#do whatever you want here
修改强>
或者你可以使用正则表达式,逗号和空格无关紧要:
import re
n = ['1993-06-11 5570',
'930611-5570',
'930611 5570',
'93 05115570',
'1993 05 11 55 70',
'1993 05 11 5570']
regex = '([0-9]+(?:[- ]?[0-9]+)*)'
match_nums = [re.search(regex, num) for num in n]
results = [i.groups() for i in match_nums]
for i in results:
print i
('1993-06-11 5570',)
('930611-5570',)
('930611 5570',)
('93 05115570',)
('1993 05 11 55 70',)
('1993 05 11 5570',)
有关正则表达式的信息,请参阅here
答案 3 :(得分:0)
建议使用with for file操作。如果它是Python 2.4或其他东西,你必须导入语句。我能想到的数字最简单的解决方案是:
from __future__ import with_statement
file_with_ssn = "/absolute/path/to/the/file"
try:
with open(file_with_ssn) as ssn_file:
for ssn in ssn_file:
ssn = filter(str.isdigit, ssn)
# This removes anything other than a number including -.
# Since SSN cannot be in negative, isdigit is fine here
numberprogram(ssn)
except EnvironmentError:
print "IOError or OSError or WindowsError happened"