且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用python检查文本文件中是否存在数据?

更新时间:2023-09-08 08:41:22

您发布的有限信息似乎总会得到0,因为您正在比较 Array (一个包含4个字符串元素的数组,每个 output_list 数组的code> FBlist 元素。我只能想象比较看起来像这样:

With the limited info you posted it would seem as though you're getting 0 always because you're comparing Array, an array with 4 string elements, with the first 2 elements of each FBlist element you appended to the output_list array. I could only imagine the comparison looking something like this:

 ["6J", "xx", "ss", "11"] == [element1, element2]

始终为False。 aw测一下test.txt文件中的内容的示例?

which will always be False. Hoaw about a sample of what's in the test.txt file?

还: int(True) int(False)总是会分别给您1或0,因此不需要 str((0,1)[found])您可以执行 str(找到)

Also: int(True) and int(False) will always give you 1 or 0 respectively so no need for str((0,1)[found]) you can just do str(found)

编辑1:

为回应您的评论,请在代码中进行此调整,以查看要比较的内容:

In response to your comment, make this adjustment in your code to see what it is you are comparing:

for line in output_list:
    print('comparing {arr} == {line} ?'.format(arr=Array, line=line[:3])
    found = int(Array == line[:3])
    result.write(found)

从您的评论中我看到迫在眉睫的问题是, Array 有4个项目,而item [:3]有3个项目,因此它们将永远不会相同...

From your comment I see an immediate problem, Array has 4 item while item[:3] has 3 item so they will never be the same...