我有一个需要以MD5格式散列的电子邮件地址的csv,然后将散列的电子邮件另存为新的csv。我还没有看到过我的确切用例,也无法成功修改现有问题。原始文件路径是 会是/ Users / [username] /Downloads/email_hashed.csv。
原始档案
email_addr
fake_email1 @ yahoo。 com
[email protected]
[email protected]
[email protected]
[email protected]
散列文件
email_addr
0x3731BF23851200A7607BA554EEAF7912
0xA5D5D3B99896D32BAC64162BD56BE177
0xAE03858BDFBDF622AF5A1852317500C3
0xC870F8D75180AC9DA2188129C910489B
0xD7AFD8085548808459BDEF8665C8D52A
评论中的答案几乎是正确的。您只需要打开另一个文件,其中写入属性 w 。我改变了你的查询来使用和,所以你不必显式关闭文件处理程序:
<$ p (/ Users / [username] /下载/email_original.csv,'rb')作为文件:
with open(/ Users / [username] / Downloads /email_hashed.csv\",'w')作为输出:
用于文件中的行:
line = line.strip()
print hashlib.md5(line).hexdigest()
output.write(hashlib.md5(line).hexdigest()+'\\\
')
I have a csv with email addresses that needs to be hashed in MD5 format, then save the hashed emails as a new csv. I haven't seen my exact use case on SO and haven't been able to successfully modify existing questions.
Original file path is "/Users/[username]/Downloads/email_original.csv" and desired output file would be "/Users/[username]/Downloads/email_hashed.csv".
Original File
email_addr [email protected] [email protected] [email protected] [email protected] [email protected]
Hashed File
email_addr 0x3731BF23851200A7607BA554EEAF7912 0xA5D5D3B99896D32BAC64162BD56BE177 0xAE03858BDFBDF622AF5A1852317500C3 0xC870F8D75180AC9DA2188129C910489B 0xD7AFD8085548808459BDEF8665C8D52A
The answer in your comment is nearly correct. You only need to open another file with the write attribute w. I have changed your query to use with so you don't to have to explicitly close the file handlers:
with open("/Users/[username]/Downloads/email_original.csv",'rb') as file: with open("/Users/[username]/Downloads/email_hashed.csv",'w') as output: for line in file: line=line.strip() print hashlib.md5(line).hexdigest() output.write(hashlib.md5(line).hexdigest() +'\n')
这篇关于MD5使用Python散列CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!