我正在制作一个程序来区分有效和无效的社会安全号码。
该程序应该能够对我计算机上的文本文件中的数字进行排序。但是我只能一次输入所有数字(我认为)。我不会让程序一一检查数字。
这就是现在的样子
def fileinput():
try:
textfile = open("numberlist.txt","r")
socialsecuritynumber = textfile.read()
numberprogram(socialsecuritynumber)
except IOError:
print("there's no such file!\n")
有人知道我该怎么做吗? 文本文件仅包含数字
- 1993-06-11 5570
- 930611-5570
- 930611 5570
- 93 05115570
- 1993年05月11日55月70日
- 1993 05 11 5570
这是我的文本文件中的数字
最佳答案
- 始终使用 with 读取文件陈述。因此,如果读取过程中出现问题,或者代码块中出现异常,文件将自动关闭。
然后使用for循环像这样逐行读取
with open("numberlist.txt","r") as textfile: for line in textfile: print line
关于Python,从文件中一行一行读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20494832/