I'm trying to combine every 2 elements of a txt file, and hash it to create a hash table, using Python. My code is as below:
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()
The file is 64KB (more than 5700 lines). I tried to run the code but it is not working nor showing any errors. The destination file (text_combined.txt) did not have anything either. Can I ask if I am doing it right or wrong?
I am new to Python as well as programming so please excuse me if I ask any bad questions. Thank you so much.
CodePudding user response:
The second f.readlines()
has nothing to read, because you've already read the entire file.
Read the file into a list variable, then iterate through the list.
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")