python--汉字字符处理
2017-06-24 16:08
239 查看
一、输出一串汉字字符串
输出:
二、输出字符串长度
输出: 18–与汉字的长度不符。
三、解码成UTF-8格式并测量长度
方法一:
方法二:
输出:6–和汉字实际的长度一致。
四、把汉字字符串一个汉字一个汉字的输出
方法一:
输出:
方法二:
输出:
方法二:
方法二:
输出:
#-*—coding:utf8-*- def txt_test(): string = u'今天你有毒!' print string txt_test()
输出:
二、输出字符串长度
#-*—coding:utf8-*- def txt_test(): string = '今天你有毒!' print string print len(string) txt_test()
输出: 18–与汉字的长度不符。
三、解码成UTF-8格式并测量长度
方法一:
#-*—coding:utf8-*- def txt_test(): string = u'今天你有毒!' print string print len(string) txt_test()
方法二:
#-*—coding:utf8-*- def txt_test(): string = '今天你有毒!' print string print len(string) print len(string.decode('utf-8')) txt_test()
输出:6–和汉字实际的长度一致。
四、把汉字字符串一个汉字一个汉字的输出
方法一:
#-*—coding:utf8-*- def txt_test(): string = u'今天你有毒!' print string print len(string) print len(string) for i in range(0,len(string),1): print string[i] txt_test()
输出:
方法二:
#-*—coding:utf8-*- def txt_test(): string = '今天你有毒!' print string print len(string) print len(string.decode('utf-8')) for i in range(0,len(string.decode('utf-8')),1): print string.decode('utf-8')[i] txt_test()
输出:
方法二:
五、判断汉字字符串里面是否有某个汉字
方法一:#-*—coding:utf8-*- def txt_test(): string = u'今天你有毒!' print string print len(string) print len(string) for i in range(0,len(string),1): print string[i] if string[i] == u'毒': print u'这句话里有个毒' txt_test()
方法二:
#-*—coding:utf8-*- def txt_test(): string = '今天你有毒!' print string print len(string) print len(string.decode('utf-8')) for i in range(0,len(string.decode('utf-8')),1): print string.decode('utf-8')[i] if string.decode('utf-8')[i] == u'毒': print '这句话里有个毒' txt_test()
输出:
相关文章推荐
- Python处理中文文本字符时提取某个汉字或字符的方法
- 汉字与unicode字符互相转换处理乱码
- python用法:处理sqlite中的中文字符时遇到的问题
- 自己改成一般处理程序的数字字符汉字验证码
- C++读写汉字,C++处理中文字符
- python判断unicode是否是汉字,数字,英文,或者其他字符
- vi在修改汉字的时候,把汉字作为一个字符处理的设置办法
- 黄聪:解决python中文处理乱码,先要弄懂“字符”和“字节”的差别
- python处理xml中非法字符的一种思路
- Python处理HTML转义字符
- python每次处理固定个数的字符的方法总结
- Python Cookbook 第二版 汉化版 [Recipe 1.1] 逐个处理字符串中的各个字符
- 解决生僻汉字的处理,你需要了解Java增补字符
- python判断unicode是否是汉字,数字,英文,或者其他字符
- python用法:处理sqlite中的中文字符时遇到的问题
- python用法:处理sqlite中的中文字符时遇到的问题
- C# substring处理中文,让汉字二个字符的处理方法
- python中的汉字处理
- python 处理中文网页时,忽略特殊字符,忽略异常
- python每次处理固定个数的字符的方法总结