Home > Software design >  python repr() function doesnt return \"
python repr() function doesnt return \"

Time:04-22

Im getting json-like string from server. It has \" instead of normal " char. When Im converting it with json.loads() it gives error.

> import json
> from_page = '{"data": "There is start: \"Hello there!\". There is end."}'
> from_page
'{"data": "There is start: "Hello there!". There is end."}'
> json.loads(from_page)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 353, in raw_decode
    obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting ',' delimiter: line 1 column 28 (char 27)

When I put \\" instead of \" it works fine.

> from_page = '{"data": "There is start: \\"Hello there!\\". There is end."}'
> from_page
'{"data": "There is start: \\"Hello there!\\". There is end."}'
> json.loads(from_page)
{'data': 'There is start: "Hello there!". There is end.'}

But problem is that I cant access data from page directly, so I cant use r"". I wanted to use repr() and then convert \ to \\ but repr() function shows " instead of \", so I cant do anything with that.

> from_page = '{"data": "There is start: \"Hello there!\". There is end."}'
> from_page
'{"data": "There is start: "Hello there!". There is end."}'
> repr(from_page)
'\'{"data": "There is start: "Hello there!". There is end."}\''

What would be the most efficient way to deal with this?

CodePudding user response:

You just can't declare your from_page variable like that, because escaped quotation mark in a regular string is just a quotation mark:

>>> var = '\"'
>>> len(var)
1

There is only one character in the var variable - the quotation mark. No backslash to convert to double backslash. So when you declare your from_page, it's content is just like displayed (note the quotation marks):

'{"data": "There is start: "Hello there!". There is end."}'
  ^----^  ^----------------^            ^---------------^               

You need to declare your string as 'raw', adding an 'r' character in front of it:

>>> from_page = r'{"data": "There is start: \"Hello there!\". There is end."}'
>> from_page
'{"data": "There is start: \\"Hello there!\\". There is end."}'
>>> json.loads(from_page)
{'data': 'There is start: "Hello there!". There is end.'}
  • Related