1. 程式人生 > >python處理編碼問題和JSON格式

python處理編碼問題和JSON格式

python trace 需要 code import strong red arch stdin

從文件讀出數據:默認utf8編碼

json.dumps()輸出數據:默認unicode編碼

json讀取(json是種通用的數據傳輸格式) import ujson as json #for performance jobj = json.loads(json_str) #type(jobj)==<type ‘dict’> json_str = json.dumps(jobj) #默認輸出unicode json.dumps(jobj, ensure_ascii=False) #輸出utf8格式 字符串做key: >>> s={} >>> s[1]=((2,3)) >>> json.dumps(s) ‘{"1":[2,3]}’ log,redis,mc_cache,hbase存儲都建議使用json格式 python -mjson.tool #json排版顯示 ultra json不支持python中long類型
: >>> import json, ujson >>> json.dumps(18446744073709551616L) ‘18446744073709551616‘ >>> ujson.dumps(18446744073709551616L) Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: long too big to convert json.dumps輸出的字符串手動粘貼置為常量,需要字符串轉義,vim操作是s/"/\\"/g 簡單介紹: http://www.ruanyifeng.com/blog/2009/05/data_types_and_json.html json格式: http://www.json.org/json-zh.html http://www.jsoneditoronline.org/ 中文編碼
def to_utf8(s): return s if isinstance(s, str) else s.encode(‘utf8‘) def to_unicode(s): return s if isinstance(s, unicode) else s.decode(‘utf8‘) 中文unicode不能寫文件 空格轉utf8後無法用strip()去除 >>> s=u‘ 有的時候,之所以哭泣,並不是因為軟弱,而是因為堅強太久。@_@search_tab‘ >>> t=u‘有的時候,之所以哭泣,並不是因為軟弱,而是因為堅強太久。@_@search_tab‘ >>> s u‘\xa0\u6709\u7684\u65f6\u5019\uff0c\u4e4b\u6240\u4ee5\u54ed\u6ce3\uff0c\u5e76\u4e0d\u662f\u56e0\u4e3a\u8f6f\u5f31\uff0c\u800c\u662f\u56e0\u4e3a\u575a\u5f3a\u592a\u4e45\u3002@_@search_tab‘ >>> t u‘\u6709\u7684\u65f6\u5019\uff0c\u4e4b\u6240\u4ee5\u54ed\u6ce3\uff0c\u5e76\u4e0d\u662f\u56e0\u4e3a\u8f6f\u5f31\uff0c\u800c\u662f\u56e0\u4e3a\u575a\u5f3a\u592a\u4e45\u3002@_@search_tab‘ >>> s.strip() u‘\u6709\u7684\u65f6\u5019\uff0c\u4e4b\u6240\u4ee5\u54ed\u6ce3\uff0c\u5e76\u4e0d\u662f\u56e0\u4e3a\u8f6f\u5f31\uff0c\u800c\u662f\u56e0\u4e3a\u575a\u5f3a\u592a\u4e45\u3002@_@search_tab‘ >>> t.strip() u‘\u6709\u7684\u65f6\u5019\uff0c\u4e4b\u6240\u4ee5\u54ed\u6ce3\uff0c\u5e76\u4e0d\u662f\u56e0\u4e3a\u8f6f\u5f31\uff0c\u800c\u662f\u56e0\u4e3a\u575a\u5f3a\u592a\u4e45\u3002@_@search_tab‘ >>> s.encode(‘utf8‘) ‘\xc2\xa0\xe6\x9c\x89\xe7\x9a\x84\xe6\x97\xb6\xe5\x80\x99\xef\xbc\x8c\xe4\xb9\x8b\xe6\x89\x80\xe4\xbb\xa5\xe5\x93\xad\xe6\xb3\xa3\xef\xbc\x8c\xe5\xb9\xb6\xe4\xb8\x8d\xe6\x98\xaf\xe5\x9b\xa0\xe4\xb8\xba\xe8\xbd\xaf\xe5\xbc\xb1\xef\xbc\x8c\xe8\x80\x8c\xe6\x98\xaf\xe5\x9b\xa0\xe4\xb8\xba\xe5\x9d\x9a\xe5\xbc\xba\xe5\xa4\xaa\xe4\xb9\x85\xe3\x80\x82@_@search_tab‘ >>> t.encode(‘utf8‘) ‘\xe6\x9c\x89\xe7\x9a\x84\xe6\x97\xb6\xe5\x80\x99\xef\xbc\x8c\xe4\xb9\x8b\xe6\x89\x80\xe4\xbb\xa5\xe5\x93\xad\xe6\xb3\xa3\xef\xbc\x8c\xe5\xb9\xb6\xe4\xb8\x8d\xe6\x98\xaf\xe5\x9b\xa0\xe4\xb8\xba\xe8\xbd\xaf\xe5\xbc\xb1\xef\xbc\x8c\xe8\x80\x8c\xe6\x98\xaf\xe5\x9b\xa0\xe4\xb8\xba\xe5\x9d\x9a\xe5\xbc\xba\xe5\xa4\xaa\xe4\xb9\x85\xe3\x80\x82@_@search_tab‘ >>> s.encode(‘utf8‘).strip() ‘\xc2\xa0\xe6\x9c\x89\xe7\x9a\x84\xe6\x97\xb6\xe5\x80\x99\xef\xbc\x8c\xe4\xb9\x8b\xe6\x89\x80\xe4\xbb\xa5\xe5\x93\xad\xe6\xb3\xa3\xef\xbc\x8c\xe5\xb9\xb6\xe4\xb8\x8d\xe6\x98\xaf\xe5\x9b\xa0\xe4\xb8\xba\xe8\xbd\xaf\xe5\xbc\xb1\xef\xbc\x8c\xe8\x80\x8c\xe6\x98\xaf\xe5\x9b\xa0\xe4\xb8\xba\xe5\x9d\x9a\xe5\xbc\xba\xe5\xa4\xaa\xe4\xb9\x85\xe3\x80\x82@_@search_tab‘ >>> t.encode(‘utf8‘).strip() ‘\xe6\x9c\x89\xe7\x9a\x84\xe6\x97\xb6\xe5\x80\x99\xef\xbc\x8c\xe4\xb9\x8b\xe6\x89\x80\xe4\xbb\xa5\xe5\x93\xad\xe6\xb3\xa3\xef\xbc\x8c\xe5\xb9\xb6\xe4\xb8\x8d\xe6\x98\xaf\xe5\x9b\xa0\xe4\xb8\xba\xe8\xbd\xaf\xe5\xbc\xb1\xef\xbc\x8c\xe8\x80\x8c\xe6\x98\xaf\xe5\x9b\xa0\xe4\xb8\xba\xe5\x9d\x9a\xe5\xbc\xba\xe5\xa4\xaa\xe4\xb9\x85\xe3\x80\x82@_@search_tab’ 參考:也談 Python 的中文編碼處理 http://in355hz.iteye.com/blog/1860787

python處理編碼問題和JSON格式