python json.loads兼容單引號數(shù)據(jù)的方法
更新時間:2018年12月19日 14:01:13 作者:BDuck2014
今天小編就為大家分享一篇python json.loads兼容單引號數(shù)據(jù)的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
Python的json模塊解析單引號數(shù)據(jù)會報錯,示例如下
>>> 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兼容單引號數(shù)據(jù)的方法就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Caffe均值文件mean.binaryproto轉(zhuǎn)mean.npy的方法
今天小編就為大家分享一篇Caffe均值文件mean.binaryproto轉(zhuǎn)mean.npy的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Django零基礎(chǔ)入門之路由path和re_path詳解
這篇文章主要介紹了Django零基礎(chǔ)入門之路由path和re_path,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09python 列表元素左右循環(huán)移動 的多種解決方案
這篇文章主要介紹了python 列表元素左右循環(huán)移動 的多種解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03Django報錯TemplateDoesNotExist的問題及解決
這篇文章主要介紹了Django報錯TemplateDoesNotExist的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08