字符串中文乱码、编解码问题


问题1:python split中文符号时报错

    weatherDict["maxWendu"]=int(weatherDict["maxWendu"].split("(")[0])

UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)

已添加

#!/usr/bin/env python 

#coding=utf-8

解决0:weatherDict["maxWendu"]=int(weatherDict["maxWendu"].encode('utf-8').split("(")[0])


1.

对需要 str->unicode 的代码,可以在前边写上

import sys

reload(sys)

sys.setdefaultencoding(‘utf8′)

把 str 编码由 ascii 改为 utf8 (或 gb18030)

2.

python3 区分了 unicode str 和 byte arrary,并且默认编码不再是 ascii



当字符串是:'\u4e2d\u56fd' 

>>>s=['\u4e2d\u56fd','\u6e05\u534e\u5927\u5b66']
>>>str=s[0].decode('unicode_escape')  #.encode("EUC_KR")
>>>print str

中国

 

js处理'\u4e2d\u56fd' :

http://www.codefans.net/jscss/code/4794.shtml

var classObj=
     {
       ToUnicode:function(str) 
       {
        return escape(str).replace(/%/g,"\\").toLowerCase();
       },
       UnUnicode:function(str)
       {
        return unescape(str.replace(/\\/g, "%"));
       },
    }



当字符串是:' 东亚学团一中'

>>>print unichr(19996)

ord()支持unicode,可以显示特定字符的unicode号码,如:

>>>print ord('A')
65

只要和Unicode连接,就会产生Unicode字串。如:

>>> 'help'
'help'
>>> 'help,' + u'python'     
u'help,python'

 

对于ASCII(7位)兼容的字串,可和内置的str()函数把Unicode字串转换成ASCII字串。如:

>>> str(u'hello world')
'hello world'

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值