python判断unicode是否是汉字,数字,英文,或者其他字符 python 判断字符串是否有中文
python \u5224\u65ad\u662f\u5426\u542b\u6709\u6570\u5b57\uff0c\u82f1\u6587\u5b57\u7b26\u548c\u6c49\u5b57str=''
\u8fd9\u91cc\u5230str\u4ee3\u8868\u4efb\u610f\u5b57\u7b26\u4e32
1.\u5224\u65ad\u662f\u5426\u542b\u6709\u6570\u5b57
if str >= u'\u4e00' and str =< u'\u9fa5':
return "\u5305\u542b\u6c49\u5b57"
else:
return "\u4e0d\u5305\u542b\u6c49\u5b57"
2.\u5224\u65ad\u4e00\u4e2aunicode\u662f\u5426\u662f\u82f1\u6587\u5b57\u6bcd
if (str>= u'\u0041' and str= u'\u0061'and str<=u'\u007a'):
return "\u5305\u542b"
else:
return "\u4e0d\u5305\u542b"
3.\u5224\u65ad\u662f\u5426\u975e\u6c49\u5b57\uff0c\u6570\u5b57\u548c\u82f1\u6587\u5b57\u7b26
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False
#!/usr/bin/env python# -*- coding:GBK -*- """\u6c49\u5b57\u5904\u7406\u7684\u5de5\u5177:\u5224\u65adunicode\u662f\u5426\u662f\u6c49\u5b57\uff0c\u6570\u5b57\uff0c\u82f1\u6587\uff0c\u6216\u8005\u5176\u4ed6\u5b57\u7b26\u3002\u5168\u89d2\u7b26\u53f7\u8f6c\u534a\u89d2\u7b26\u53f7\u3002"""def is_chinese(uchar): """\u5224\u65ad\u4e00\u4e2aunicode\u662f\u5426\u662f\u6c49\u5b57""" if uchar >= u'/u4e00' and uchar= u'/u0030' and uchar= u'/u0041' and uchar= u'/u0061' and uchar0x7e: #\u4e0d\u662f\u534a\u89d2\u5b57\u7b26\u5c31\u8fd4\u56de\u539f\u6765\u7684\u5b57\u7b26 return uchar if inside_code==0x0020: #\u9664\u4e86\u7a7a\u683c\u5176\u4ed6\u7684\u5168\u89d2\u534a\u89d2\u7684\u516c\u5f0f\u4e3a:\u534a\u89d2=\u5168\u89d2-0xfee0 inside_code=0x3000 else: inside_code+=0xfee0 return unichr(inside_code)def Q2B(uchar): """\u5168\u89d2\u8f6c\u534a\u89d2""" inside_code=ord(uchar) if inside_code==0x3000: inside_code=0x0020 else: inside_code-=0xfee0 if inside_code0x7e: #\u8f6c\u5b8c\u4e4b\u540e\u4e0d\u662f\u534a\u89d2\u5b57\u7b26\u8fd4\u56de\u539f\u6765\u7684\u5b57\u7b26 return uchar return unichr(inside_code)def stringQ2B(ustring): """\u628a\u5b57\u7b26\u4e32\u5168\u89d2\u8f6c\u534a\u89d2""" return "".join([Q2B(uchar) for uchar in ustring])def uniform(ustring): """\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\uff0c\u5b8c\u6210\u5168\u89d2\u8f6c\u534a\u89d2\uff0c\u5927\u5199\u8f6c\u5c0f\u5199\u7684\u5de5\u4f5c""" return stringQ2B(ustring).lower()def string2List(ustring): """\u5c06ustring\u6309\u7167\u4e2d\u6587\uff0c\u5b57\u6bcd\uff0c\u6570\u5b57\u5206\u5f00""" 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 retListif __name__=="__main__": #test Q2B and B2Q for i in range(0x0020,0x007F): print Q2B(B2Q(unichr(i))),B2Q(unichr(i)) #test uniform ustring=u'\u4e2d\u56fd \u4eba\u540da\u9ad8\u9891A' ustring=uniform(ustring) ret=string2List(ustring) print ret\u98760
你看长度就可以了。length1 = len(unicode_string)
length2 = len(unicode_string.encode('utf-8'))
if length1 == length2: 这说明是英文的,否则就是汉字
数字判断用isdigit()这个函数就可以了
绛旓細杈撳嚭鍒板睆骞曟椂瀹藉害涓11涓瓧绗﹂敭鐩樿緭鍏ヤ竴涓9800鍒9811涔嬮棿鐨勬鏁存暟n锛屼綔涓Unicode缂栫爜锛屾妸n-1銆乶鍜宯+1涓変釜Unicode缂栫爜瀵瑰簲瀛楃鎸夌収鏍煎紡瑕佹眰杈撳嚭鍒板睆骞曪細瀹藉害涓11涓瓧绗︺
绛旓細s鏄瓧绗︿覆s = '袪褍褋褋泻懈泄 褟蟹褘泻'#鎵撳嵃s瀵瑰簲鐨unicode鐮乸rint(s.encode('unicode_escape').decode('utf-8'))#a鏄痷nicode鐮乤 = r'\u0420\u0443\u0441\u0441\u043a\u0438\u0439 \u044f\u0437\u044b\u043a'#鎵撳嵃a瀵瑰簲鐨勫瓧绗︿覆print(a.encode("utf-8").decode('unicode_...
绛旓細杩欎釜閿欒閫氬父鏄敱浜Python瑙i噴鍣ㄦ棤娉曞皢杈撳叆鐨勫瓧鑺傚簭鍒楄В鐮佷负Unicode瀛楃涓诧紝鑰屽鑷寸殑銆傚畠閫氬父鏄洜涓虹紪鐮佷笉鍖归厤瀵艰嚧鐨勶紝姣斿鍦℅BK缂栫爜涓嬭緭鍏ヤ簡涓涓棤娉曡В鐮佺殑瀛楄妭銆傝В鍐虫闂鐨勬柟娉曟槸灏哖ython瑙i噴鍣ㄧ殑缂栫爜璁剧疆涓哄尮閰嶈緭鍏ョ殑缂栫爜銆傚彲浠ヤ娇鐢ㄤ互涓嬫柟娉曟潵瑙e喅璇ラ棶棰橈細鍦ㄧ粓绔垨鎺у埗鍙拌緭鍏ヤ互涓嬪懡浠よ缃甈ython瑙i噴鍣ㄧ殑缂栫爜涓...
绛旓細Python涓濡備綍鍒ゆ柇涓涓瓧绗︿覆涓湁鍑犱釜绌烘牸http://zhidao.baidu.com/question/138854675106454205.html"""from __future__ import (print_function, unicode_literals)text = '2014.11 2016.03 xxx鏈夐檺鍏徃 (1骞4涓湀)'currentCharIsSpace = Falsecount = 0for c in text: if currentCharIsSpace: if c.isspace()...
绛旓細棣栧厛锛屽湪python涓瀛楃涓茬殑琛ㄧず鏄 鐢unicode缂栫爜銆傛墍浠ュ湪鍋氱紪鐮佽浆鎹㈡椂锛岄氬父瑕佷互unicode浣滀负涓棿缂栫爜銆俤ecode鐨勪綔鐢ㄦ槸灏嗗叾浠栫紪鐮佺殑瀛楃涓茶浆鎹㈡垚unicode缂栫爜锛屾瘮濡 a.decode('utf-8')锛岃〃绀哄皢utf-8缂栫爜鐨勫瓧绗︿覆杞崲鎴恥nicode缂栫爜 encode鐨勪綔鐢ㄦ槸灏唘nicode缂栫爜鐨勫瓧绗︿覆杞崲鎴愬叾浠栫紪鐮佹牸寮忕殑瀛楃涓诧紝姣斿b.encode...
绛旓細Python涓锛寀琛ㄧずunicodestring锛岃〃绀轰娇鐢╱nicode杩涜缂栫爜锛屾病鏈塽琛ㄧずbytestring,绫诲瀷鏄痵tr锛屽湪娌℃湁澹版槑缂栫爜鏂瑰紡鏃讹紝榛樿ASCI缂栫爜銆傚鏋滆鎸囧畾缂栫爜鏂瑰紡锛屽彲鍦ㄦ枃浠堕《閮ㄥ姞鍏ョ被浼煎涓嬩唬鐮侊細#-*-coding:utf-8-*- utf-8鏄竴绉嶆敮鎸佷腑鏂囩殑缂栫爜鏍煎紡銆傚瓧姣嶅墠鍔爎琛ㄧずrawstring锛屼篃鍙師濮嬪瓧绗︿覆甯搁噺銆備竴鑸敤鍦ㄤ竴涓嬩袱...
绛旓細'gbk' codec can't encode character u'\u3232' in position 0: illegal multibyte sequence 浣嗘槸锛屾湰韬殑纭凡缁忔槸杞崲濂戒簡unicode瀛楃涓蹭簡銆傝鎯呭彲鍙傝冿細銆愭暣鐞嗐Python涓锛屽浣曞皢鍙嶆枩鏉爑绫诲瀷锛圽uXXXX锛夌殑瀛楃涓诧紝杞崲涓哄搴旂殑unicode鐨勫瓧绗 锛堟澶勪笉鑳借创鍦板潃锛岃鐢╣oogle鎼滄爣棰橈紝鍗冲彲鎵惧埌甯栧瓙鍦板潃锛...
绛旓細浠鎴朥寮澶寸殑瀛楃涓茶〃绀unicode瀛楃涓诧紝print鏃讹紝灏辩浉褰撲簬print "娓告垙缁撴灉\n鎴愮哗鍒楄〃"锛屽瓧绗︿覆涓紝杞箟瀛楃浼氳繘琛岃浆涔夈傝緭鍑虹粨鏋滃氨鏄細娓告垙缁撴灉 鎴愮哗鍒楄〃 鍒涓绉嶇敤娉曪紝濡傛灉鎯崇洿鎺ヨ緭鍑"娓告垙缁撴灉\n鎴愮哗鍒楄〃"锛屽氨鍐欐垚:aa=(r"娓告垙缁撴灉\n鎴愮哗鍒楄〃")print(aa)杩欐牱锛孿n灏变笉浼氳繘琛岃浆涔夛紝鏂逛究杈撳嚭澶勭悊鍙...
绛旓細涓涓瓧姣峮锛岃屼笉鏄〃绀烘崲琛屼簡銆備互r寮澶寸殑瀛楃锛屽父鐢ㄤ簬姝e垯琛ㄨ揪寮忥紝瀵瑰簲鐫re妯″潡銆備互u鎴朥寮澶寸殑瀛楃涓茶〃绀unicode瀛楃涓 Unicode鏄功鍐欏浗闄呮枃鏈殑鏍囧噯鏂规硶銆傚鏋滀綘鎯宠鐢ㄩ潪鑻辫鍐欐枃鏈,閭d箞浣犻渶瑕佹湁涓涓敮鎸乁nicode鐨勭紪杈戝櫒銆傜被浼煎湴,Python鍏佽浣犲鐞哢nicode鏂囨湰鈥斺斾綘鍙渶瑕佸湪瀛楃涓插墠鍔犱笂鍓嶇紑u鎴朥銆
绛旓細鍐呭濡備笅锛 import sys reload(sys) sys.setdefaultencoding('utf8')閲嶅惎缂栬瘧鍣ㄥ悗锛岃繍琛宻ys.getdefaultencoding()锛屽氨浼氬彂鐜扮紪鐮佸凡鎴愬彉鎴恥tf-8浜嗐傚嚭鐜伴敊璇垎鏋愶細鐢变簬python鍦ㄥ畨瑁呮椂锛岄粯璁ょ殑缂栫爜鏄痑scii銆傚綋鍑虹幇闈瀉scii瀛楃鏃讹紝灏卞嚭鎶ラ敊銆傗滀腑鍥解濇槸瀛楃涓诧紝python鑷姩鍏堣В鐮佸皢璧疯浆鎹负unicode,鐒跺悗鍐峞ncode...