简体   繁体   English

ValueError:使用ast.literal_eval的格式错误的字符串

[英]ValueError: malformed string using ast.literal_eval

I'm doing a loop to get json api, here is what I have in my loop: 我正在做一个循环来获取json api,这是我在循环中的内容:

response_item = requests.request('GET',url_item,params=None,verify=False)
response_item = json.loads(response_item.text)
response_item = ast.literal_eval(json.dumps(response_item, ensure_ascii=False).encode('utf8'))

I scan around 45000 json objects, I generate "url_item" variable for each iteration. 我扫描45000个json对象,我为每次迭代生成“url_item”变量。 Each object is the same, I can get something like 7000 object and I have the following error when I reach the 7064th: 每个对象都是一样的,我可以得到像7000对象的东西,当我到达7064th时我有以下错误:

Traceback (most recent call last):
  File "C:\Python27\tools\api_item.py", line 47, in <module>
    response_item = ast.literal_eval(json.dumps(response_item, ensure_ascii=False).encode('utf8'))
  File "C:\Python27\lib\ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "C:\Python27\lib\ast.py", line 63, in _convert
    in zip(node.keys, node.values))
  File "C:\Python27\lib\ast.py", line 62, in <genexpr>
    return dict((_convert(k), _convert(v)) for k, v
  File "C:\Python27\lib\ast.py", line 63, in _convert
    in zip(node.keys, node.values))
  File "C:\Python27\lib\ast.py", line 62, in <genexpr>
    return dict((_convert(k), _convert(v)) for k, v
  File "C:\Python27\lib\ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

I used to print the second and third "response_item". 我曾经打印过第二个和第三个“response_item”。 Of course in this case the third one isn't displayed since I have the error just before, here what I have for the print after the json.load: 当然在这种情况下,第三个没有显示,因为我之前有错误,这里是我在json.load之后的打印:

{u'restrictions': [], u'name': u'Sac \xe0 dos de base', u'level': 0, u'rarity': u'Basic', u'vendor_value': 11, u'details': {u'no_sell_or_sort': False, u'size': 20}, u'game_types': [u'Activity', u'Wvw', u'Dungeon', u'Pve'], u'flags': [u'NoSell', u'SoulbindOnAcquire', u'SoulBindOnUse'], u'icon': u'https://render.guildwars2.com/file/80E36806385691D4C0910817EF2A6C2006AEE353/61755.png', u'type': u'Bag', u'id': 8932, u'description': u'Un sac de 20 emplacements pour les personnages d\xe9butants.'}

Every item I get before this one has the same type, same format, and I don't have any error except for the 7064th ! 我在此之前获得的每个项目都具有相同的类型,相同的格式,除了7064th之外我没有任何错误!

Thank you for your help! 谢谢您的帮助!

You should not use ast.literal_eval() on JSON data. 应该使用ast.literal_eval()的JSON数据。 JSON and Python literals may look like the same thing, but they are very much not. JSON和Python文字看起来可能是一样的,但它们却不是。

In this case, your data contains a boolean flag, set to false in JSON. 在这种情况下,您的数据包含一个布尔标志,在JSON中设置为false A proper Python boolean uses title-case, so False : 一个合适的Python布尔值使用title-case,所以False

>>> import json, ast
>>> s = '{"no_sell_or_sort": false, "size": 20}'
>>> json.loads(s)
{u'no_sell_or_sort': False, u'size': 20}
>>> ast.literal_eval(s)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/ast.py", line 80, in literal_eval
    return _convert(node_or_string)
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/ast.py", line 63, in _convert
    in zip(node.keys, node.values))
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/ast.py", line 62, in <genexpr>
    return dict((_convert(k), _convert(v)) for k, v
  File "/Users/mj/Development/Library/buildout.python/parts/opt/lib/python2.7/ast.py", line 79, in _convert
    raise ValueError('malformed string')
ValueError: malformed string

Other differences include using null instead of None , and Unicode escape sequences in what to Python 2 looks like a plain (bytes) string , using UTF-16 surrogates when escaping non-BMP codepoints. 其他差异包括使用null而不是None ,而Python 2中的Unicode转义序列看起来像普通(字节)字符串 ,在转义非BMP代码点时使用UTF-16代理。

Load your data with json.loads() , not ast.literal_eval() . 使用json.loads()而不是ast.literal_eval()加载数据。 Not only will it handle proper JSON just fine, it is also faster . 它不仅可以正常处理正确的JSON,而且速度更快

In your case, it appears you are using json.dumps() then try to load the data again with ast.literal_eval() . 根据你的情况,似乎你正在使用json.dumps()然后尝试再次与加载数据ast.literal_eval() There is no need for that step, you already had a Python object. 没有必要这一步,你已经有了一个Python对象。

In other words, the line: 换句话说,该行:

response_item = ast.literal_eval(json.dumps(response_item, ensure_ascii=False).encode('utf8'))

is redundant at best, and very, very wrong, at worst. 在最坏的情况下,充其量是多余的,非常非常错误。 Re-encoding response_item to a JSON string does not produce something that can be interpreted as a Python literal. response_item重新编码为JSON字符串不会产生可以解释为Python文字的内容。

ast.literal_eval is safe with SQL injection if you are using this. 如果你使用它,ast.literal_eval是安全的SQL注入。 because when an unwanted charter is inserted it will show Syntex error which prevents from an injection. 因为当插入不需要的章程时,它将显示Syntex错误,这会阻止注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ast.literal_eval ValueError(&#39;格式错误的字符串&#39;) - ast.literal_eval ValueError('malformed string') ValueError:使用ast.literal_eval时格式错误的字符串 - ValueError: malformed string when using ast.literal_eval 带有元组字符串表示的格式错误的字符串 ValueError ast.literal_eval() - Malformed String ValueError ast.literal_eval() with String representation of Tuple ast.literal_eval() ValueError: 格式错误的节点或字符串 - ast.literal_eval() ValueError: malformed node or string python:ast.literal_eval()提供ValueError:格式错误的字符串 - python: ast.literal_eval() gives ValueError: malformed string ValueError: 格式错误的节点或字符串 ast.literal_eval() - ValueError: malformed node or string with ast.literal_eval() 使用ast.literal_eval时格式错误的字符串 - Malformed string while using ast.literal_eval ValueError:添加Keras层时格式错误的节点或带有ast.literal_eval()的字符串 - ValueError: malformed node or string with ast.literal_eval() when adding a Keras layer 当我使用 discord.py 使用 ast.literal_eval 时,第 1 行出现格式错误的节点或字符串 - malformed node or string on line 1 when I use ast.literal_eval using discord.py ast.literal_eval引发ValueError - ast.literal_eval throwing ValueError
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM