0

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

4
  • stackoverflow.com/questions/13940272/… Commented Feb 9, 2018 at 16:07
  • Is the url you are hitting s_url returning JSON or plain text? Commented Feb 9, 2018 at 16:07
  • @EricWilson s_url return plain text Commented Feb 9, 2018 at 16:12
  • Seems you can just use r.text then, right? I'm not sure what your problem is. Commented Feb 9, 2018 at 16:20

1 Answer 1

1

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:

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.