gpt4 book ai didi

python - 值错误 : Invalid\escape: error while doing json. 加载()

转载 作者:太空宇宙 更新时间:2023-11-04 00:24:05 25 4
gpt4 key购买 nike

我正在抓取一个网站,它有这个 JSON 数据作为返回。

https://pastebin.com/R50eTqrD这是 print( repr( string ) ) 的输出 https://pastebin.com/VH6JrDMG这是 print( string )

的输出

我在做

resp = json.loads(resp)

但是它给了我这个错误

ValueError: Invalid \escape: line 1 column 170 (char 169)

我找到了解决方案 here它建议我做

resp = json.loads(HTMLParser().unescape(resp.decode('unicode-escape')))

但它现在抛出这个错误

UnicodeEncodeError: 'ascii' codec can't encode characters in position 51-59: ordinal not in range(128)

我已经尝试了几种解决方案,比如

json.loads(HTMLParser().unescape(resp.decode('unicode-escape')).encode("utf-8"))

还有很多,但没有一个对我有用。

最佳答案

字符串中的那些 \x3E 字符有问题。如果 s 包含字符串,试试这个:

json.loads(s.replace(r'\x3E', '\x3E'))

关于python - 值错误 : Invalid\escape: error while doing json. 加载(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48072862/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com