我试图将txt文件的每两个元素组合起来,并使用Python创建哈希表。我的代码如下:
import hashlib
def SHA1_hash(string):
hash_obj = hashlib.sha1(string.encode())
return(hash_obj.hexdigest())
with open("/Users/admin/Downloads/Project_files/dictionary.txt") as f:
text_file = open("/Users/admin/Downloads/Project_files/text_combined.txt", "w",encoding = 'utf-8')
for i in f.readlines():
for j in f.readlines():
text_c = i.strip() + j.strip()
n = text_file.write(SHA1_hash(text_c) + "\n")
text_file.close()文件为64 is (超过5700行)。我试图运行这段代码,但它没有工作,也没有显示任何错误。目标文件(text_combined.txt)也没有任何内容。我能问一下我做得对还是错?
我对Python和编程都很陌生,所以如果我问任何不好的问题,请原谅。非常感谢。
发布于 2022-04-16 04:03:48
第二个f.readlines()没有什么可读取的,因为您已经读取了整个文件。
将文件读入列表变量,然后遍历列表。
with open("/Users/admin/Downloads/Project_files/dictionary.txt") as f, open("/Users/admin/Downloads/Project_files/text_combined.txt", "w",encoding = 'utf-8') as textfile:
lines = f.readlines():
for i in lines:
for j in lines:
text_c = i.strip() + j.strip()
n = text_file.write(SHA1_hash(text_c) + "\n")https://stackoverflow.com/questions/71890962
复制相似问题