workingFile = ("fileOfNumbs")
fp = open(workingFile)
这是我所拥有的。我尝试了许多不同的方法,但没有一种方法可行。
答案 0 :(得分:0)
我不确定文本文件是什么样的,所以我猜错了:
如果文本文件是这样的:
1
2
3
4
然后你可以这样做:
required_numbers = range(max_number)
f = open("fileOfNumbs.txt", "r")
data = f.read()
f.close()
for i in data.split('\n'):
if int(i) not in required_numbers:
print(i)
输出:
3
答案 1 :(得分:-1)
可能性......但下次还要尝试:
# -*- coding: utf-8 -*-
max = 4000
f = open("file.txt", "r") # Open the file
data = f.readlines() # Return a list with each element being a line
f.close() # Close the file
data = [int(line[:len(line)-2]) for line in data] # Remove the "\n" at the end of each line.
# Also converts to an int the lines.
missing = list() # Initialize a list of missing elements
for i in range(max):
if i in data:
continue
else:
missing.append(list)
print (missing)
或者采用Gnoliz下面提出的解决方案,将required_numbers
替换为list(range(max))
,将print
替换为您需要的任何内容。