Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.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中应该做的事情_Python_Comparison - Fatal编程技术网

&引用==&引用;没有做我认为在Python中应该做的事情

&引用==&引用;没有做我认为在Python中应该做的事情,python,comparison,Python,Comparison,在我正在编写的脚本中,我执行以下操作: data["serial"] = md5.new(json.dumps(data["data"])).hexdigest() ... ... lst = serialFile.readlines() if(len(lst) > 0): lastLine = lst[len(lst) - 1] ... ... print "lLn : " , lastLine print "md5 : " , str(data["se

在我正在编写的脚本中,我执行以下操作:

data["serial"] = md5.new(json.dumps(data["data"])).hexdigest()
...
...
lst = serialFile.readlines()
    if(len(lst) > 0):
        lastLine = lst[len(lst) - 1]     
...
...
print "lLn : " , lastLine
print "md5 : " , str(data["serial"])
if lastLine == data["serial"]:
我可以在打印lLn和md5时验证它们是否相同。我觉得我应该把逻辑语句弄错了。。。但我不

我还尝试使用:

if lastLine == str(data["serial"]):
我对Python还是相当陌生的。。。这里缺少什么?

假设
readlines()
是在打开的文件句柄上完成的,它会将整个流读取到一个行列表中,行包括回车符和换行符。您希望在行上调用
rstrip()
,以删除可能存在的所有额外换行符和回车符。如果在要删除的结尾还有其他空白字符,则需要调用
rstrip('\r\n')

另一个提示:您不需要找到列表的长度来获取最后一个元素,您只需在您的情况下执行
lst[-1]

把这一切放在一起,你会做一些

lst = serialFile.readlines()
    if(len(lst) > 0):
        lastLine = lst[-1].rstrip('\r\n')

可能在
lastLine
的末尾有一个换行符。请尝试打印
repr(lastLine)
repr(data[“serial”])
。lLn:e1a9502f3fcd6ec352bb10a1fd8153dd md5:'e1a9502f3fcd6ec352bb10a1fd8153dd'平台行分隔符(包括
\r\n
)被转换为
\n
,您只需要去除这一个字符。