Reputation: 1
i will be very appreciated for any help
I'm getting info from website... And generally everyting works fine... But, just look at code:
r = requests.get(s_url)
print r.text
>>>[{"nameID":"D1","text":"I’ll understand what actually happened here."}]
print r.json()
>>>[{u'nameID':u'D1',u'text':u'I\u2019ll understand what actually happened
here.'"}]
As you see after r.json() ' was changed to \u2019. Why it did it? How I can force to don't make such changes?
Thanks
Upvotes: 0
Views: 188
Reputation: 9664
It is not an apostrophe ('
) in your string, but a (unicode) character "right single quotation mark" (’
). Which may not be as obvious to a naked eye (depending on a font used easier or more difficult to spot), but it is still a completely different character to the computer.
>>> u"’" == u"'"
False
The difference you see between displaying text
attributed and what json()
method returns is just a matter of representation of that character.
For instance:
>>> s = u'’'
>>> print s
’
But when you do the same with a dictionary returned by json()
individual keys and values thereof are formatted using repr()
resulting in:
>>> d = {'k': s}
>>> print d
{'k': u'\u2019'}
Just as in:
>>> print repr(s)
u'\u2019'
Nonetheless, it is still the same thing:
>>> d['k'] == s
True
For that matter:
>>> u'’' == u'\u2019'
True
You may want to have a look at __str__()
and __repr__()
methods as well as description of print
for more details:
Upvotes: 1