Python检查TXT文件中是否存在数字
我不太明白为什么我的python脚本不能识别文本文件中的数字 这是文本文件:Python检查TXT文件中是否存在数字,python,raspberry-pi,Python,Raspberry Pi,我不太明白为什么我的python脚本不能识别文本文件中的数字 这是文本文件: 001420999; 000502999; 000120999; 000126999; 001220499; 001180000; 001104799; 001123111; 这是我正在使用的代码: 以open('/var/www/html/capcodes.txt',r')作为f: capcode=capcode.split() 对于cap代码中的cap: 如果f.read()中的cap.strip(): 打印(“
001420999;
000502999;
000120999;
000126999;
001220499;
001180000;
001104799;
001123111;
这是我正在使用的代码:
以open('/var/www/html/capcodes.txt',r')作为f:
capcode=capcode.split()
对于cap代码中的cap:
如果f.read()中的cap.strip():
打印(“真实”)
其他:
打印(“假”)
是的,capcodes
被填满了,他确实毫无问题地得到了cap.strip()
它变得越来越奇怪,唯一一次它说真的是数字:“001180000”其他的不起作用。
.read
读取整个文件。调用一次后,您就到了文件的末尾。在循环开始之前运行read()一次,并将结果存储在变量中,以便:
with open('/var/www/html/capcodes.txt', 'r') as f:
capcodes = capcode.split()
stored_lines = f.read()
for cap in capcodes:
if cap.strip() in stored_lines:
print("true")
else:
print("false")
请注意,如果要查找完整的行匹配,可能需要先进行更多清理,并将文件放入列表中:
with open('/var/www/html/capcodes.txt', 'r') as f:
capcodes = capcode.split()
stored_lines = [line.strip() for line in f.readlines()]
for cap in capcodes:
if cap.strip() in stored_lines:
print("true")
else:
print("false")
…否则,如果文件中有行“12345”,则cap的值“123”将匹配。
.read
读取整个文件。调用一次后,您就到了文件的末尾。在循环开始之前运行read()一次,并将结果存储在变量中,以便:
with open('/var/www/html/capcodes.txt', 'r') as f:
capcodes = capcode.split()
stored_lines = f.read()
for cap in capcodes:
if cap.strip() in stored_lines:
print("true")
else:
print("false")
请注意,如果要查找完整的行匹配,可能需要先进行更多清理,并将文件放入列表中:
with open('/var/www/html/capcodes.txt', 'r') as f:
capcodes = capcode.split()
stored_lines = [line.strip() for line in f.readlines()]
for cap in capcodes:
if cap.strip() in stored_lines:
print("true")
else:
print("false")
…否则,如果文件中有行“12345”,cap的值“123”将匹配。文件指针位于第一个
f.read()
之后的文件末尾。文件指针位于第一个f.read()之后的文件末尾。
。