mercredi 22 avril 2015

If I compare two strings in python I get false even if they are the same

I am trying to compare two strings, one downloaded, one from a file, but the if-statement returns always false, even if the strings are equal.

Am I doing something wrong? Is this a bug in Python?

Code:

#!/usr/bin/python
import json
import urllib2


jsonstring = urllib2.urlopen("http://ift.tt/1JtErMZ").read()
j = json.loads(jsonstring)
current_xkcd = j['num']
print current_xkcd
with open ("xkcd.num", "r") as file:
        downloaded_xkcd = file.read().replace('\n', '')
print downloaded_xkcd

if current_xkcd == downloaded_xkcd:
        print "Already got latest xkcd"
else:
        print "Downloading xkcd..."

Output:

1515
1515
Downloading xkcd...

Aucun commentaire:

Enregistrer un commentaire