導航:首頁 > 編程語言 > python判斷含中文文字

python判斷含中文文字

發布時間:2022-08-05 18:13:48

1. python 判斷是不是中文字

法一:

isinstance(s, str) 用來判斷是否為一般字元串

isinstance(s, unicode) 用來判斷是否為unicode



if type(str).__name__!="unicode":
str=unicode(str,"utf-8")
else:
pass

法二:

Python chardet 字元編碼判斷
使用 chardet 可以很方便的實現字元串/文件的編碼檢測。尤其是中文網頁,有的頁面使用GBK/GB2312,有的使用UTF8,如果你需要去爬一些頁面,知道網頁編碼很重要的,雖然HTML頁面有charset標簽,但是有些時候是不對的。那麼chardet就能幫我們大忙了。

chardet實例
>>> import urllib
>>> rawdata = urllib.urlopen('http://www.google.cn/').read()
>>> import chardet
>>> chardet.detect(rawdata)
{'confidence': 0.98999999999999999, 'encoding': 'GB2312'}
>>>chardet可以直接用detect函數來檢測所給字元的編碼。函數返回值為字典,有2個元數,一個是檢測的可信度,另外一個就是檢測到的編碼。

chardet 安裝
下載chardet後,解壓chardet壓縮包,直接將chardet文件夾放在應用程序目錄下,就可以使用import chardet開始使用chardet了。

或者使用setup.py安裝文件,將chardet拷貝到Python系統目錄下,這樣所有的python程序只要用import chardet就可以了。

2. 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'沒有包含中文'

3. python 判斷是否有中文字元

根據GB2312-80標准,每個漢字的機內碼由二個位元組組成,每個位元組的最高位均為1。
是以程序可以判斷:

#include<stdio.h>
int main()
{int i,k=1,j=0;
unsigned char s[100];
gets(s);
for(i=0;s[i];i++)
if(s[i]>128){k=0;j++;}
if(j==i)printf("\"%s\"全部是由漢字組成\n",s);
else if(k)printf("\"%s\"中沒有中文\n",s);
else printf("\"%s\"中有部分漢字\n",s);
system("pause");
}

4. python 文件是否含有中文

python判斷是否是中文需要滿足u'[u4e00-u9fa5]+',需要注意如果正則表達式的模式中使用unicode,那麼要匹配的字元串也必須轉換為unicode,否則肯定會不匹配。


zhPattern = re.compile(u'[u4e00-u9fa5]+')


示例代碼:

#-*-coding:utf-8-*-
importre
zhPattern=re.compile(u'[u4e00-u9fa5]+')
contents=u'判斷一段文本中是否包含簡體中:'
match=zhPattern.search(contents)
ifmatch:
printu'有中文:%s'%(match.group(0),)
else:
printu'沒有包含中文'

5. Python判斷字元串中是否有中文字元

首先,在Python中字元串的表示是 用unicode編碼。所以在做編碼轉換時,通常要以unicode作為中間編碼。
decode的作用是將其他編碼的字元串轉換成unicode編碼,比如 a.decode('utf-8'),表示將utf-8編碼的字元串轉換成unicode編碼
encode的作用是將unicode編碼的字元串轉換成其他編碼格式的字元串,比如b.encode('utf-8'),表示將unicode編碼格式轉換成utf-8編碼格式的字元串

判斷一個字元串中是否含有中文字元:
好了,有了以上知識,就可以很容易的解決這個問題了。這是代碼

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 結果:
19 True
20 False
21 True

6. 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

7. 14python 判斷字元串中是否含有漢字

1. 判斷字元串中是否含有漢字。

def has_hz(text):

hz_yes = False

for ch in text:

if isinstance(ch, unicode):

if unicodedata.east_asian_width(ch)!= 'Na':

hz_yes = True

break

else:

continue

return hz_yes

def has_hz(text):

hz_yes = False

for ch in text:

if isinstance(ch, unicode):

if unicodedata.east_asian_width(ch)!= 'Na':

hz_yes = True

break

else:

continue

return hz_yes

單元測試:

assert not has_hz("")

assert not has_hz(" ")

assert not has_hz("123")

assert not has_hz(u"123abc")

assert has_hz(u"123abc漢字")

assert has_hz(u"漢字")

assert not has_hz("")

assert not has_hz(" ")

assert not has_hz("123")

assert not has_hz(u"123abc")

assert has_hz(u"123abc漢字")

assert has_hz(u"漢字")

8. python判斷是否含有中文字元及長度

1
2
3
4

#coding=utf-8

test_str = u'提問123'
print len(test_str) # 輸出5

或者

1
2
3
4
5

#coding=utf-8

test_str = '提問123'
test_str_unicode = test_str.decode('utf-8')
print len(test_str_unicode) # 輸出5

求這種長度可以轉化成求解碼(unicode)的長度;報UnicodeDecodeError,應該是直接用了test_str.encode('utf-8'),這是編碼。

閱讀全文

與python判斷含中文文字相關的資料

熱點內容
好興動app還款怎麼登錄不上去了 瀏覽:665
鄭州雲伺服器託管 瀏覽:722
伺服器地址跟蹤 瀏覽:980
免費google雲伺服器 瀏覽:516
摘譯和編譯的英文 瀏覽:359
熱泵壓縮機選型 瀏覽:121
op手機微信加密如何解除 瀏覽:386
如何在王牌戰爭找到高爆率伺服器 瀏覽:13
江浙小學語文輔導課用什麼APP 瀏覽:99
新夢幻大陸伺服器地址 瀏覽:241
網吧伺服器怎麼更換壁紙 瀏覽:530
linux命令方法 瀏覽:332
linux下載freetype 瀏覽:123
程序員入駐平台 瀏覽:327
程序員大戰外掛 瀏覽:745
html實例教程pdf 瀏覽:157
linux命令開放所有許可權 瀏覽:575
30歲能學會編程 瀏覽:737
小火箭的伺服器是什麼 瀏覽:967
cad查信息命令 瀏覽:402