python - 字符串比较不起作用!语言是python
问题描述
import hashlib
import os
dir_path = input("Input directory : ") + "/"
hashset_path = input("Input hashset directory (ex: C:/../user/hashset.txt) : ")
hash_type = int(input("1:MD5 2.SHA-1 3.SHA-256\n choose hash : "))
if hash_type == 1:
hash_type = "md5"
elif hash_type == 2:
hash_type = "sha1"
elif hash_type == 3:
hash_type = "sha256"
else:
print("somethig is wrong")
exit()
temp = getattr(hashlib, hash_type)
f1 = open(hashset_path,'r',encoding='UTF-8')
lines = f1.readlines()
for line in lines:
line = line.strip()
f1.close()
# compare file
file_lst = os.listdir(dir_path)
count = 0
matchedfile = []
for file in file_lst:
filepath = dir_path + file
f = open(filepath, 'rb')
data = f.read()
f.close()
for n in range(0,len(lines)):
if (str(temp(data).hexdigest()) == str(lines[n])):
matchedfile.append("filename : " + file + " " + "calculated hash : " + temp(data).hexdigest() + " " + "hash of hashset.txt : " + lines[n])
count += 1
if count == 0:
print("no matched hash")
else:
for i in range(0,len(matchedfile)):
print(matchedfile[i])
我的代码有什么问题?
if (str(temp(data).hexdigest()) == str(lines[n])):
此代码不起作用..甚至两个字符串都完全相同!我能怎么做..
解决方案
您似乎正在从文本文件中读取。请注意,此过程包括您实际上并未剥离的行尾:
for line in lines:
line = line.strip()
上面的代码片段除了浪费时间之外什么也没做。
你的意思是这样写:
lines = [line.strip() for line in lines]
推荐阅读
- matlab - 链接到 gfortran 以获取 mex 文件
- spring-cloud - 项目构建错误:org.springframework.cloud:spring-cloud-starter-config:jar 的“dependencies.dependency.version”缺失
- wso2-am - 使用外部 OpenID Connect 身份提供程序进行 WSO2 存储访问
- javascript - 这是什么错误 ERROR 由于出现错误无法准备测试。TypeError:无法读取未定义的属性“日期”?
- javascript - 缺少一个条件
- php - 如何修复'SQLSTATE [22007]:无效的日期时间格式:1292 截断不正确的 DOUBLE 值:'X''
- authentication - 我对 Kubernetes 部署有疑问。任何人都可以帮助我在尝试连接到集群 IP 时总是遇到此错误
- javascript - ReactJS - 如何检测一个元素是否是 ReactJS 中其父级的第 n 个_第一个_或最后一个子级?
- scala - 使用 akka-http 记录响应正文时出现错误“Deflater 已关闭”
- javascript - React:在 React 中使用简单的函数进行 A/B 测试有什么问题吗?