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
Python: How to Calculate the Average Word Length in a Sentence Using the .Split Command
Printing Simple Diamond Pattern in Python
Sklearn Error Valueerror: Input Contains Nan, Infinity or a Value Too Large for Dtype('Float64')
Print a List of Space-Separated Elements
How to Locate the Input Within Div
Extract Values from Column of Dictionaries Using Pandas
Jupyter Notebook, Python3 Print Function: No Output, No Error
How to Execute Multiple Commands in a Single Session in Paramiko - Python
Find Out the Percentage of Missing Values in Each Column in the Given Dataset
Convert the String 2.90K to 2900 or 5.2M to 5200000 in Pandas Dataframe
How to Read Image Data from a Url in Python
Python Pip Install Error [Ssl: Certificate_Verify_Failed]
Check If a Python Script Is Already Running in Windows
Shifting the Elements of an Array in Python