Valueerror: Invalid \Escape Unable to Load Json from File

ValueError: Invalid \escape: When readin json as respons in Scrapy

First of all, +1 for scraping the mobile API. Much more clever than scraping from HTML!

Indeed there is a issue with the encoding.There are some octal encoded characters ([...] \074br/\076\074br/\076Best Regards,\074br/\076Emily [...]) that breaks the JSON parsing. To get rid of them use:

response.body.decode('unicode-escape')

Also there are some encoded HTML characters in the data: "“Nice clean and perfectly average”". I suggest to unescape them:

from HTMLParser import HTMLParser
...
json.loads(HTMLParser().unescape(response.body.decode('unicode-escape'))
...

In Python 3:

import html 
...
json.loads(html.unescape(response.body.decode('unicode-escape')))

The result should look like: [{'title': '“Nice clean and perfectly average”', 'bodyfull': '[...] stay. <br/><br/>Best Regards,<br/>Emily Rodriguez", [...]}]

As you see, there is some HTML tags in the result. If you want to remove the HTML tags you could use a RegEx like:

import re
...
p = re.compile(r'<.*?>')
no_html = p.sub('', str_html))

JSONDecodeError; Invalid /escape when parsing from Python

Hi you need to use double (backslashes), remove the last comma in the objects property and finally you dont need close the file inside the with block

[
{
"frame_id":1,
"filename":"C:\\Yolo_v4\\darknet\\build\\darknet\\x64\\f047.png",
"objects": [
{"class_id":32, "name":"right", "relative_coordinates":{"center_x":0.831927, "center_y":0.202225, "width":0.418463, "height":0.034752}, "confidence":0.976091},
{"class_id":19, "name":"h", "relative_coordinates":{"center_x":0.014761, "center_y":0.873551, "width":0.041723, "height":0.070544}, "confidence":0.484339},
{"class_id":24, "name":"left", "relative_coordinates":{"center_x":0.285694, "center_y":0.200752, "width":0.619584, "height":0.032149}, "confidence":0.646595}
]
}
]

Unable to load json containing escape sequences

Using json.dumps() to see how json would represent your target string:

>>> orig = { 'a' : 'val\ue' }
>>> jstring = json.dumps(orig)
>>> print jstring
{"a": "val\\ue"}
>>> extracted = json.loads(jstring)
>>> print extracted
{u'a': u'val\\ue'}
>>> print extracted['a']
val\ue
>>>

This was in Python 2.7.3, though, so it may be only partially relevant to your Python 3.x environment. Still, I don't think JSON has changed that much...

Python json.loads fails with `ValueError: Invalid control character at: line 1 column 33 (char 33)`

The problem is your unicode string contains carriage returns (\r) and newlines (\n) within a string literal in the JSON data. If they were meant to be part of the string itself, they should be escaped appropriately. If they weren't meant to be part of the string, they shouldn't be in your JSON either.

If you can't fix where you got this JSON string to produce valid JSON, you could either remove the offending characters:

>>> json.loads(s.replace('\r\n', ''))

or escape them manually:

>>> json.loads(s.replace('\r\n', '\\r\\n'))

simplejson.loads() get Invalid \escape: 'x'

JSON has no hex escape (\xNN) like some languages (including JavaScript) and notations do, details here. It has a unicode escape, \uNNNN where NNNN is four hex digits, but no \x hex escape.

Invalid control character with Python json.loads

There is no error in your json text.

You can get the error if you copy-paste the string into your Python source code as a string literal. In that case \n is interpreted as a single character (newline). You can fix it by using raw-string literals instead (r'', Use triple-quotes r'''..''' to avoid escaping "' quotes inside the string literal).



Related Topics



Leave a reply



Submit