python处理编码问题和JSON格式

从文件读出数据:默认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
 
简单介绍:
json格式:
 
中文编码
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 的中文编码处理

转载于:https://www.cnblogs.com/yaoyaohust/p/10363099.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值