python字符串包含中文
‘壹’ 请教python匹配中文字符的方法
#-*-coding:UTF-8-*-
__author__=u'丽江海月客栈'
s="""{"hearl":"","nickname":"","loginstatus":"","loginstate":"","tip":"未注册服务","idUser":"","sessionId":"","upgradeUrl":"","checkCodeKey":"false"}"""
ss=s.decode('utf-8')
importre
re_words=re.compile(u"[u4e00-u9fa5]+")
m=re_words.search(ss,0)
printm.group()
‘贰’ Python字符串是什么,如何使用
字符串的表示
字符串可以被成对的单引号(single quote)或双引号(double quotes)包围起来,这两者的作用是一样的:
更多关于Python的基础性知识可以看下这个网页的视频教程,Python常见的数据类型及使用方法掌握,希望我的回答能帮到你。
‘叁’ python 判断字符串中是否含有汉字
#!
/usr/bin/python
#
-*-
coding:
utf-8
-*-
import
re
zhPattern
=
re.compile(u'[\u4e00-\u9fa5]+')
#一个小应用,判断一段文本中是否包含简体中:
contents=u'一个小应用,判断一段文本中是否包含简体中:'
match
=
zhPattern.search(contents)
if
match:
print
u'有中文:%s'
%
(match.group(0),)
else:
print
u'没有包含中文'
‘肆’ Python 编码转换与中文处理
python 中的 unicode 是让人很困惑、比较难以理解的问题. 这篇文章 写的比较好, utf-8是 unicode的一种实现方式,unicode、gbk、gb2312是编码字符集.
Python 默认脚本文件都是 ANSCII 编码的,当文件 中有非 ANSCII 编码范围内的字符的时候就要使用" 编码指示 "来修正一个 mole 的定义中,如果.py文件中包含中文字符(严格的说是含有非anscii字符),则需要在第一行或第二行指定编码声明: # -*- coding=utf-8 -*- 或者 #coding=utf-8
其他的编码如:gbk、gb2312也可以;否则会出现:
先说一下python中的字符串类型,在python中有两种字符串类型,分别是 str 和 unicode ,他们都是basestring的派生类;
在str的文档中有这样的一句话:
也就是说在读取一个文件的内容,或者从网络上读取到内容时,保持的对象为str类型;如果想把一个str转换成特定编码类型,需要把str转为Unicode,然后从unicode转为特定的编码类型如:utf-8、gb2312等。
unicode 转为 gb2312,utf-8等,使用 encode(encoding)
utf-8,GBK转换为 unicode 使用 unicode(s,encoding) 或者 s.decode(encoding)
普通的 str 转为 unicode,
如果直接执行s.encode('gb2312')会发生什么?
这里会发生一个异常:Python 会自动的先将 s 解码为 unicode ,然后再编码成 gb2312。因为解码是python自动进行的,我们没有指明解码方式,python 就会使用 sys.defaultencoding 指明的方式来解码。很多情况下 sys.defaultencoding 是 ANSCII,如果 s 不是这个类型就会出错。
拿上面的情况来说,我的 sys.defaultencoding 是 anscii,而 s 的编码方式和文件的编码方式一致,是 utf8 的,所以出错了:
对于这种情况,我们有两种方法来改正错误:
s = '中文'
s.decode('utf-8').encode('gb2312') ```
import sys
reload(sys) # Python2.5 初始化后会删除 sys.setdefaultencoding 这个方法,我们需要重新载入
sys.setdefaultencoding('utf-8')
str = '中文'
str.encode('gb2312')
print open("Test.txt").read()
import codecs
print open("Test.txt").read().decode("utf-8")
Traceback (most recent call last):
File "ChineseTest.py", line 3, in <mole>
print open("Test.txt").read().decode("utf-8")
UnicodeEncodeError: 'gbk' codec can't encode character u'ufeff' in position 0: illegal multibyte sequence
import codecs
data = open("Test.txt").read()
if data[:3] == codecs.BOM_UTF8:
data = data[3:]
print data.decode("utf-8")
s = "中文"
print unicode(s, "utf-8")
Traceback (most recent call last):
File "ChineseTest.py", line 3, in <mole>
s = unicode(s, "utf-8")
UnicodeDecodeError: 'utf8' codec can't decode bytes in position 0-1: invalid data
s = "中文"
print unicode(s, "gbk")
s = "中文"
print unicode(s, "cp936")
‘伍’ python 判断是否含有数字,英文字符和汉字
str=''
这里到str代表任意字符串
1.判断是否含有数字
if str >= u'\u4e00' and str =< u'\u9fa5':
return "包含汉字"
else:
return "不包含汉字"
2.判断一个unicode是否是英文字母
if (str>= u'\u0041' and str<=u'\u005a') or (str >= u'\u0061'and str<=u'\u007a'):
return "包含"
else:
return "不包含"
3.判断是否非汉字,数字和英文字符
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False
‘陆’ Python中中文字符串怎么处理
如果处理的字符串中出现中文表示的字符,要想不出错,就得转成unicode编码了。具体的方法有:
1、decode(),将其他边编码的字符串转换成unicode编码,如str1.decode('gb2312'),表示将gb2312编码的字符串str1转换成unicode编码;
2、encode(),将unicode编码转换成其他编码的字符串,如str2.encode('gb2312'),表示将unicode编码的字符串str2转换成gb2312编码;
3、unicode(),同decode(),将其他编码的字符串转换成unicode编码,如unicode(str3, 'gb2312'),表示将gb2312编码的字符串str3转换成unicode编码。
转码的时候一定要先搞明白字符串str是什么编码,然后decode成unicode,最后再encode成其他编码。
另外,对一个unicode编码的字符串在进行解码会出错,所以在编码未知的情况下要先判断其编码方式是否为unicode,可以用isinstance(str, unicode)。
不仅是中文,以后处理含非ascii编码的字符串时,都可以遵循以下步骤:
1、确定源字符的编码格式,假设是utf8;
2、使用unicode()或decode()转换成unicode编码,如str1.decode('utf8'),或者unicode(str1, 'utf8');
3、把处理后字符串用encode()编码成指定格式。