我正在制作一个程序,它能区分有效和无效的社会保险号码。在
这个程序应该能够从我电脑上的文本文件中对数字进行排序。但是我只能一次输入所有的数字(我想)。我不会让程序逐一核对数字。在
这就是现在的样子
def fileinput():
try:
textfile = open("numberlist.txt","r")
socialsecuritynumber = textfile.read()
numberprogram(socialsecuritynumber)
except IOError:
print("there's no such file!\n")
有人知道我该怎么做吗? 文本文件只包含数字
这是我文本文件中的数字
如果行中有不需要的空格(或者只是万一有):
^{pr2}$如果你发现数字后面有逗号或其他东西,你可以这样做:
^{3}$编辑:
或者,可以使用正则表达式,逗号和空格无关紧要:
有关正则表达式的信息,请参见here
然后使用for循环逐行读取,如下所示
按你的建议与fourtheye一起使用。您可以使用readLines()方法,并使用for-in循环逐个迭代这些行并检查其有效性。不保证你的大文件不会被破坏。在
相关问题 更多 >
编程相关推荐