MD5使用Python散列CSV [英] MD5 Hashing a CSV with Python

查看:112
本文介绍了MD5使用Python散列CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要以MD5格式散列的电子邮件地址的csv,然后将散列的电子邮件另存为新的csv。我还没有看到过我的确切用例,也无法成功修改现有问题。原始文件路径是/ Users / [用户名] /Downloads/email_original.csv和所需的输出文件 会是/ Users / [username] /Downloads/email_hashed.csv



原始档案

  email_addr 
fake_email1 @ yahoo。 com
fake_email2@gmail.com
fake_email3@college.edu
fake_email4@hotmail.com
fake_email5@ford.com



散列文件

  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
fake_email1@yahoo.com
fake_email2@gmail.com
fake_email3@college.edu
fake_email4@hotmail.com
fake_email5@ford.com

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆