python json.loads namespace_python json.loads兼容单引号数据的方法
Python的json模塊解析單引號數據會報錯,示例如下
>>> import json
>>> data = "{'field1': 0, 'field2': 'hehehehe', 'field3': 'hahaha'}"
>>> json.loads(data)
Traceback (most recent call last):
File “”, line 1, in
File “/usr/lib/python3.5/json/init.py”, line 319, in loads
return _default_decoder.decode(s)
File “/usr/lib/python3.5/json/decoder.py”, line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File “/usr/lib/python3.5/json/decoder.py”, line 355, in raw_decode
obj, end = self.scan_once(s, idx)
json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)
摸索的解決辦法如下
>>> data = json.dumps(eval(data))
>>> print(data)
{“field3”: “hahaha”, “field2”: “hehehehe”, “field1”: 0}
處理后正確解析
>>> print(json.loads(data))
{‘field3': ‘hahaha', ‘field2': ‘hehehehe', ‘field1': 0}
以上這篇python json.loads兼容單引號數據的方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
總結
以上是生活随笔為你收集整理的python json.loads namespace_python json.loads兼容单引号数据的方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全球超算TOP500:美国突破百亿亿次、
- 下一篇: python数据预处理_Python数据