Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python检查TXT文件中是否存在数字_Python_Raspberry Pi - Fatal编程技术网

Python检查TXT文件中是否存在数字

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(): 打印(“

我不太明白为什么我的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():
打印(“真实”)
其他:
打印(“假”)
是的,
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()之后的文件末尾。