python--汉字字符处理
来源:互联网 发布:知豆电动汽车加盟代理 编辑:程序博客网 时间:2024/05/09 23:54
一、输出一串汉字字符串
#-*—coding:utf8-*-def txt_test(): string = u'今天你有毒!' print stringtxt_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()
输出:
阅读全文
0 0
- python--汉字字符处理
- 汉字字符处理
- c++汉字字符处理
- python中的汉字处理
- python汉字的处理
- Python处理中文文本字符时提取某个汉字或字符的方法
- 中文字符,汉字处理,避免乱码
- C++读写汉字,C++处理中文字符
- python处理中文字符
- python 字符处理
- Python 字符处理
- Python处理中文字符
- python 字符编码处理
- 中文字符处理 python
- Python 字符处理
- python处理中文字符
- Python处理字符技巧
- python字符处理
- 查询数据库,表,表结构
- storm集群部署和配置过程详解
- order by的使用
- Kotlin-17.枚举类(enum class)
- bzoj 1046: [HAOI2007]上升序列
- python--汉字字符处理
- grub 恢复启动
- 拉流推流服务器设计
- ubuntu 16.04 设置root用户初始密码
- 飞行器设计大作业
- leetcode 623 Add One Row to Tree
- imageNamed和imageWithContentsOfFile
- 对于Servlet的get请求和post请求的两种数据请求的编码格式
- python:打印直角三角形