Solution: Python Md5 Hashing, trailing newline when reading file

Development | Programming languages
  • 5.0 (1 ratings)
Last updated 12/2018
Description
I am experiencing unexpected behavior with file-reading and hashing (in Python 3.7). I have a file that simply has the text "helloworld" in it, without a newline at the end:
import hashlib

def hashit(inp):
    return hashlib.md5(inp.encode('utf-8')).hexdigest()

from_var = 'helloworld'

with open('input.txt', 'r') as fo:
    from_file = fo.read()

print(f' from_file      : { repr(from_file) }')
print(f' from_var       : { repr(from_var) }')

print(f' from_file hash : { hashit(from_file) }')
print(f' from_var  hash : { hashit(from_var) }')
helloworld

Attachments
Solutions
Edit input.txt nad delete second line, and run hashit.py again:
 from_file      : 'helloworld'
 from_var       : 'helloworld'
 from_file hash : fc5e038d38a57032085441e7fe7010b0
 from_var  hash : fc5e038d38a57032085441e7fe7010b0


on November 28th, 2018 (9:45 pm)
What masters said
You must sign in to comment!!
All comments
thumb up
on December 05th, 2018 (10:23 pm)
We use cookies to give you the best possible experience on our site. By continuing to use the site you agree to our use of cookies. Find out more Accept