关于python中json load出来编码为unicode的问题的解决
2016-10-11 00:29
856 查看
技术方法来源网址:
http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-ones-from-json-in-python
昨天遇到一个问题:
把一个字典 用json.dump(f,data)到文件里面
然后再从另外一端json.load(f)取出字典
发现字典的键值和内容都成了unicode
例如:{u"name":u"value"}
原因:
查阅以后发现是python写入时候编码都先中间转化成了unicode
如果说我需要不带u的东西
我要解码
网上有很多:
我试了下,下面是两个都可以生效的方案
1.
2 用json 的hook函数来解决
信息来源参考:
http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-ones-from-json-in-python
http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-ones-from-json-in-python
昨天遇到一个问题:
把一个字典 用json.dump(f,data)到文件里面
然后再从另外一端json.load(f)取出字典
发现字典的键值和内容都成了unicode
例如:{u"name":u"value"}
原因:
查阅以后发现是python写入时候编码都先中间转化成了unicode
如果说我需要不带u的东西
我要解码
网上有很多:
我试了下,下面是两个都可以生效的方案
1.
>>> import json >>> import yaml >>> list_org = ['a', 'b']>>> list_dump = json.dumps(list_org)>>> list_dump '["a" 4000 , "b"]'>>> json.loads(list_dump)[u'a', u'b']>>> yaml.safe_load(list_dump)['a', 'b'] |
import json def json_load_byteified(file_handle): return _byteify( json.load(file_handle, object_hook=_byteify), ignore_dicts=True ) def json_loads_byteified(json_text): return _byteify( json.loads(json_text, object_hook=_byteify), ignore_dicts=True ) def _byteify(data, ignore_dicts = False): # if this is a unicode string, return its string representation if isinstance(data, unicode): return data.encode('utf-8') # if this is a list of values, return list of byteified values if isinstance(data, list): return [ _byteify(item, ignore_dicts=True) for item in data ] # if this is a dictionary, return dictionary of byteified keys and values # but only if we haven't already byteified it if isinstance(data, dict) and not ignore_dicts: return { _byteify(key, ignore_dicts=True): _byteify(value, ignore_dicts=True) for key, value in data.iteritems() } # if it's anything else, return it in its original form return data
信息来源参考:
http://stackoverflow.com/questions/956867/how-to-get-string-objects-instead-of-unicode-ones-from-json-in-python
相关文章推荐
- 关于python中json load出来编码为unicode的问题的解决
- 解决python3.6下scrapy中xpath.extract()匹配出来的内容转成json与.csv文件没有编码(unicode)的问题
- 解决python中json模块loads出来的结构都是unicode的问题
- python编码问题——解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX
- Python处理unicode编码的txt文件(Python中文处理)——解决to_excel()和to_csv()导出文件内容为空的问题
- Python3 解决编码问题: `UnicodeEncodeError: 'gbk' codec can't encode character '\xa0' in position 10: ille
- 关于python的str和unicode以及编码的问题
- python编码问题——解决python3 UnicodeEncodeError: 'gbk' codec can't encode character '\xXX' in position XX
- python2.7 的中文编码处理,解决UnicodeEncodeError: 'ascii' codec can't encode character 问题
- 解决Python写入文件编码问题(UnicodeEncodeError: 'ascii' codec can't encode characters in...)
- python 输出JSON类型数据时遇到的编码问题(utf8,unicode)
- 【python学习】编码时写路径使用后斜杠导致的unicode error问题解决办法
- 关于python3里gbk编码的问题解决
- 关于python3,numpy-loadtxt的编码问题
- 如何解决Python中的UnicodeEncodeError编码错误问题
- fcitx 关于多语言,不同编码的一点说明;同时也可以用于解决fcitx不能输入问题
- JsonCpp中文unicode编码问题
- python utf编码(\u5029\u5973\u5e7d\u9b42)的奇怪问题与解决
- python-json-javascript之间关于字符集的转换问题
- 关于VC多字节字符集和unicode宽字符集的使用和问题,只有明白了其中的原理,才能灵活解决问题