Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MD5 Hashing a CSV with Python

Tags:

python

csv

hash

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
like image 623
Frank B. Avatar asked Dec 08 '25 00:12

Frank B.


1 Answers

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')
like image 106
Alex Avatar answered Dec 13 '25 05:12

Alex



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!