我有个档案PhaseMar.txt文件每一行只包含
4
16
14
44
55
34
它需要检查是否有一个特定的数字让我们假设16存在于该文件或没有。你知道吗
我正在使用
test_file = open('PhaseMar.txt', 'r') #modification!
test_lines = test_file.readlines() #modification!
print test_lines
size =len(test_lines) #[0]
print size
count=0
for i in xrange(1,size):
#print int(realID)
#print int(test_lines[i])
print (int(test_lines[i])-int(realID))
if abs(int(test_lines[i])-int(realID))> 0.1:
count=count+1
else:
count=0
if (count>0):
print "true"
else:
print "false"
count=0
它检查所有的条目6次,它给我的答案是真的,当数字存在。但是,它也会打印所有的错误。我想知道是否有一个班轮存在这个。 谨致问候
假设检查文件中的整数16PhaseMars.txt文件,如下所示的快速解决方案,但由于需要将整个文件读入内存,因此处理大文件的内存效率不高
如果您需要处理大文件,itertools可能很方便,它只在每次迭代需要处理时读取文件中的每一行。你知道吗
例如,假设我们对16以上的数字感兴趣 导入itertools
相关问题 更多 >
编程相关推荐