首先,在Python中字符串的表示是 用unicode編碼。所以在做編碼轉(zhuǎn)換時(shí),通常要以u(píng)nicode作為中間編碼。
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),荔城企業(yè)網(wǎng)站建設(shè),荔城品牌網(wǎng)站建設(shè),網(wǎng)站定制,荔城網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營銷,網(wǎng)絡(luò)優(yōu)化,荔城網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。
decode的作用是將其他編碼的字符串轉(zhuǎn)換成unicode編碼,比如 a.decode('utf-8'),表示將utf-8編碼的字符串轉(zhuǎn)換成unicode編碼
encode的作用是將unicode編碼的字符串轉(zhuǎn)換成其他編碼格式的字符串,比如b.encode('utf-8'),表示將unicode編碼格式轉(zhuǎn)換成utf-8編碼格式的字符串
判斷一個(gè)字符串中是否含有中文字符:
好了,有了以上知識(shí),就可以很容易的解決這個(gè)問題了。這是代碼
1 #-*- coding:utf-8 -*-
2
3 import sys
4 reload(sys)
5 sys.setdefaultencoding('utf8')
6
7 def check_contain_chinese(check_str):
8 for ch in check_str.decode('utf-8'):
9 if u'\u4e00' = ch = u'\u9fff':
10 return True
11 return False
12
13 if __name__ == "__main__":
14 print check_contain_chinese('中國')
15 print check_contain_chinese('xxx')
16 print check_contain_chinese('xx中國')
17
18 結(jié)果:
19 True
20 False
21 True
#!/usr/bin/env python
# -*- coding:GBK -*-
"""漢字處理的工具:
判斷unicode是否是漢字,數(shù)字,英文,或者其他字符。
全角符號(hào)轉(zhuǎn)半角符號(hào)。"""
__author__="internetsweeper zhengbin0713@gmail.com"
__date__="2007-08-04"
def is_chinese(uchar):
"""判斷一個(gè)unicode是否是漢字"""
if uchar = u'\u4e00' and uchar=u'\u9fa5':
return True
else:
return False
def is_number(uchar):
"""判斷一個(gè)unicode是否是數(shù)字"""
if uchar = u'\u0030' and uchar=u'\u0039':
return True
else:
return False
def is_alphabet(uchar):
"""判斷一個(gè)unicode是否是英文字母"""
if (uchar = u'\u0041' and uchar=u'\u005a') or (uchar = u'\u0061' and uchar=u'\u007a'):
return True
else:
return False
def is_other(uchar):
"""判斷是否非漢字,數(shù)字和英文字符"""
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False
def B2Q(uchar):
"""半角轉(zhuǎn)全角"""
inside_code=ord(uchar)
if inside_code0x0020 or inside_code0x7e: #不是半角字符就返回原來的字符
return uchar
if inside_code==0x0020: #除了空格其他的全角半角的公式為:半角=全角-0xfee0
inside_code=0x3000
else:
inside_code+=0xfee0
return unichr(inside_code)
def Q2B(uchar):
"""全角轉(zhuǎn)半角"""
inside_code=ord(uchar)
if inside_code==0x3000:
inside_code=0x0020
else:
inside_code-=0xfee0
if inside_code0x0020 or inside_code0x7e: #轉(zhuǎn)完之后不是半角字符返回原來的字符
return uchar
return unichr(inside_code)
def stringQ2B(ustring):
"""把字符串全角轉(zhuǎn)半角"""
return "".join([Q2B(uchar) for uchar in ustring])
def uniform(ustring):
"""格式化字符串,完成全角轉(zhuǎn)半角,大寫轉(zhuǎn)小寫的工作"""
return stringQ2B(ustring).lower()
def string2List(ustring):
"""將ustring按照中文,字母,數(shù)字分開"""
retList=[]
utmp=[]
for uchar in ustring:
if is_other(uchar):
if len(utmp)==0:
continue
else:
retList.append("".join(utmp))
utmp=[]
else:
utmp.append(uchar)
if len(utmp)!=0:
retList.append("".join(utmp))
return retList
if __name__=="__main__":
#test Q2B and B2Q
for i in range(0x0020,0x007F):
print Q2B(B2Q(unichr(i))),B2Q(unichr(i))
#test uniform
ustring=u'中國 人名a高頻A'
ustring=uniform(ustring)
ret=string2List(ustring)
print ret
以上轉(zhuǎn)自
這個(gè)問題是做 MkIV 預(yù)處理程序時(shí)搞定的,就是把一個(gè)混合了中英文混合字串分離為英文與中文的子字串,譬如,將 ”我的 English 學(xué)的不好“ 分離為 “我的"、" English ” 與 "學(xué)的不好" 三個(gè)子字串。
1. 中英文混合字串的統(tǒng)一編碼表示中英文混合字串處理最省力的辦法就是把它們的編碼都轉(zhuǎn)成 Unicode,讓一個(gè)漢字與一個(gè)英文字母的內(nèi)存位寬都是相等的。這個(gè)工作用 Python 來做,比較合適,因?yàn)?Python 內(nèi)碼采用的是 Unicode,并且為了支持 Unicode 字串的操作,Python 做了一個(gè) Unicode 內(nèi)建模塊,把 string 對(duì)象的全部方法重新實(shí)現(xiàn)了一遍,另外提供了 Codecs 對(duì)象,解決各種編碼類型的字符串解碼與編碼問題。
譬如下面的 Python 代碼,可實(shí)現(xiàn) UTF-8 編碼的中英文混合字串向 Unicode 編碼的轉(zhuǎn)換:# -*-
coding:utf-8 -*-
a = "我的 English 學(xué)的不好"
print type(a),len (a), a
b = unicode (a, "utf-8")
print type(b), len (b), b字符串 a 是 utf-8 編碼,使用 python 的內(nèi)建對(duì)象 unicode 可將其轉(zhuǎn)換為 Unicode 編碼的字符串 b。上述代碼執(zhí)行后的輸出結(jié)果如下所示,比較字串 a 與字串 b 的長(zhǎng)度,顯然 len (b) 的輸出結(jié)果是合理的。type 'str' 27 我的 English 學(xué)的不好
type 'unicode' 15 我的 English 學(xué)的不好要注意的一個(gè)問題是 Unicode 雖然號(hào)稱是“統(tǒng)一碼”,不過也是存在著兩種形式,即:
UCS-2:為 16 位碼,具有 2^16 = 65536 個(gè)碼位; UCS-4:為 32 位碼,目前的規(guī)定是其首字節(jié)的首位為 0,因此具有 2^31 = 2147483648 個(gè)碼位,不過現(xiàn)在的只使用了 0x00000000 - 0x0010FFFF 之間的碼位,共 1114112 個(gè)。
使用Python sys 模塊提供的一個(gè)變量 maxunicode 的值可以判斷當(dāng)前 Python 所使用的 Unicode 類型是 UCS-2 的還是 UCS-4 的。import sys
print sys.maxunicode若 sys.maxunicode 的值為 1114111,即為 UCS-4;若為 65535,則為 UCS-2。
2. 中英文混合字串的分離一旦中英文字串的編碼獲得統(tǒng)一,那么對(duì)它們進(jìn)行分裂就是很簡(jiǎn)單的事情了。首先要為中文字串與英文字串分別準(zhǔn)備一個(gè)收集器,使用兩個(gè)空的字串對(duì)象即可,譬如 zh_gather 與 en_gather;然后要準(zhǔn)備一個(gè)列表對(duì)象,負(fù)責(zé)按分離次序存儲(chǔ) zh_gather 與 en_gather 的值。下面這個(gè) Python 函數(shù)接受一個(gè)中英文混合的 Unicode 字串,并返回存儲(chǔ)中英文子字串的列表。def split_zh_en (zh_en_str):
zh_en_group = []
zh_gather = ""
en_gather = ""
zh_status = False
for c in zh_en_str:
if not zh_status and is_zh (c):
zh_status = True
if en_gather != "":
zh_en_group.append ([mark["en"],en_gather])
en_gather = ""
elif not is_zh (c) and zh_status:
zh_status = False
if zh_gather != "":
zh_en_group.append ([mark["zh"], zh_gather])
if zh_status:
zh_gather += c
else:
en_gather += c
zh_gather = ""
if en_gather != "":
zh_en_group.append ([mark["en"],en_gather])
elif zh_gather != "":
zh_en_group.append ([mark["zh"],zh_gather])
return zh_en_group上述代碼所實(shí)現(xiàn)的功能細(xì)節(jié)是:對(duì)中英文混合字串 zh_en_str 的遍歷過程中進(jìn)行逐字識(shí)別,若當(dāng)前字符為中文,則將其添加到 zh_gather 中;若當(dāng)前字符為英文,則將其添加到 en_gather 中。zh_status 表示中英文字符的切換狀態(tài),當(dāng) zh_status 的值發(fā)生突變時(shí),就將所收集的中文子字串或英文子字串添加到 zh_en_group 中去。
判斷字串 zh_en_str 中是否包含中文字符的條件語句中出現(xiàn)了一個(gè) is_zh () 函數(shù),它的實(shí)現(xiàn)如下:def is_zh (c):
x = ord (c)
# Punct Radicals
if x = 0x2e80 and x = 0x33ff:
return True
# Fullwidth Latin Characters
elif x = 0xff00 and x = 0xffef:
return True
# CJK Unified Ideographs
# CJK Unified Ideographs Extension A
elif x = 0x4e00 and x = 0x9fbb:
return True
# CJK Compatibility Ideographs
elif x = 0xf900 and x = 0xfad9:
return True
# CJK Unified Ideographs Extension B
elif x = 0x20000 and x = 0x2a6d6:
return True
# CJK Compatibility Supplement
elif x = 0x2f800 and x = 0x2fa1d:
return True
else:
return False這段代碼來自 jjgod 寫的 XeTeX 預(yù)處理程序。
對(duì)于分離出來的中文子字串與英文子字串,為了使用方便,在將它們存入 zh_en_group 列表時(shí),我對(duì)它們分別做了標(biāo)記,即 mark["zh"] 與 mark["en"]。mark 是一個(gè) dict 對(duì)象,其定義如下:mark = {"en":1, "zh":2}如果要對(duì) zh_en_group 中的英文字串或中文字串進(jìn)行處理時(shí),標(biāo)記的意義在于快速判定字串是中文的,還是英文的,譬如:for str in zh_en_group:
if str[0] = mark["en"]:
do somthing
else:
do somthing
num = "4" # 全角數(shù)字num.isdigit() # Truenum.isdecimal() # Truenum.isnumeric() # Truenum = b"4" # bytenum.isdigit() # Truenum.isdecimal() # AttributeError: bytes object has no attribute isdecimalnum.isnumeric() # AttributeError: bytes object has no attribute isdecimalnum = "IV" # 羅馬數(shù)字num.isdigit() # Truenum.isdecimal() # Falsenum.isnumeric() # True#num = "四" # 漢字num.isdigit() # Falsenum.isdecimal() # Falsenum.isnumeric() # True# isdigit() 支持 全角數(shù)字,byte數(shù)字(單字節(jié)) 羅馬數(shù)字 不支持漢字# isdecimal() 支持全角數(shù)字 , 不支持 羅馬數(shù)字 漢字 , 報(bào)錯(cuò) byte數(shù)字(單字節(jié))# isnumeric() 支持 全角數(shù)字 羅馬數(shù)字 漢字 報(bào)錯(cuò) byte數(shù)字(單字節(jié))通常使用最多的是isdecimal ,如果想了解更多可以來傳智播客軟件測(cè)試學(xué)科學(xué)習(xí)
1、判斷字符串是否含有某字符,區(qū)分大小寫。
2、判斷字符串是否含有某英文字符,區(qū)分大小寫,可用SEARCH函數(shù)代替FIND函數(shù)。
3、判斷字符串中是否包含多個(gè)指定字符中的某一個(gè)。
4、判斷是否同時(shí)包含某幾個(gè)字符。
5、判斷包含某幾個(gè)字符中的幾個(gè),可結(jié)合引用COUNT()函數(shù)。
當(dāng)前標(biāo)題:python判斷漢字函數(shù) python 判斷漢字
標(biāo)題路徑:http://chinadenli.net/article22/dogsjcc.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供服務(wù)器托管、移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站收錄、商城網(wǎng)站、虛擬主機(jī)、網(wǎng)站導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)