user1479836
user1479836

Reputation: 93

stdout comparison to a hash result (python)

using python i create a hash result for a file. i retrieve a hash result from somewhere else, but i have to retrieve it using stdout, and i would like to compare the two. however the retrieved stdout is retrieved as a list, and has a \n at the end. if i print it it might look, for example, like this: [6d52f\n]

i know that the problem is that if i try to add the \n to my current hash result, it automatically disregards the \n to try to compare the two (like i do below in my code), so i'm currently a bit unsure about how i can compare the two. i know the answer is probably staring me in the face, but i would appreciate any help.

my code is:

if ("%s\n" % thishash == otherhash):
    print "they are the same"
else:
    print "they are not the same"

Upvotes: 1

Views: 87

Answers (3)

apatrushev
apatrushev

Reputation: 425

Just apply strip to otherhash:

if thishash == otherhash.strip():
    print "they are the same"
else:
    print "they are not the same"

Some additional recommendations:

  1. Don't use insignificant bracket. Be pythonic.
  2. Probably lowercasing otherhash could be a good idea.

Upvotes: 0

jfs
jfs

Reputation: 414625

I have to retrieve it using stdout

You might mean "stdin", not "stdout".

other_hash = raw_input() # no trailing newline
if this_hash == other_hash: 
   "same"

Or you could call other_hash.rstrip() without arguments to remove all trailing whitespace.

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174662

Simply strip the newline from the stdin:

>>> x = ['6d52f\n']
>>> x[0].rstrip('\n')
'6d52f'

Upvotes: 1

Related Questions