導航:首頁 > 編程語言 > python如何判斷字元串中含有中文

python如何判斷字元串中含有中文

發布時間:2022-03-31 12:29:19

A. 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"漢字")

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

C. 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");
}

D. python 判斷字元串中是否只有中文字元

def is_chinese(s):
rt = False
if s>= u"\u4e00" and s<= u"\u9fa6":
rt = True
return rt

ss = "中文測試測試請說ha".decode("utf-8")

for row in ss:
print row,is_chinese(row)

E. python 判斷字元串中是否只有中文字元

1
2
3
4
5

def is_chinese(s):
if s >= u'\u4e00' and s<=u'\u9fa5':
return True
else:
return False

給你這個判斷中文字元的函數,用到字元串上就可以了。

F. python 判斷字元串中是否只有中文字元

ef is_chinese(s):
rt = False
if s>= u"\u4e00" and s<= u"\u9fa6":
rt = True
return rt

ss = "中文測試測試請說ha".decode("utf-8")

for row in ss:
print row,is_chinese(row)

G. 在python中如何判斷字元串中是否有繁體中文

基於文本文檔(Markdown) 設想好需要的基本需要的表、欄位、類型;使用 Rails Migration 隨著功能的開發逐步創建表;隨著細節功能的開發、需求,逐步增加欄位,刪除欄位,或者調整欄位類型;第一個 Release 的時候清理 Migrations 合並成一個;隨著後期的改動,逐步增加、修改、刪除欄位或表。基本上我的所有項目都是這么搞的,這和項目是否復雜無關。所以我前面為什麼說思路需要轉變。

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

I. python 判斷字元串是否是一段中文

嘻嘻,渣度機器人隊再得隊1分。13級巨神又遭調戲。

J. 如何判斷一個字元串中含有中文

可以這樣簡單地判斷:
普通字元串,其組成是由ASCII碼小於等於127的字元,當字元串類型是由有符號的char類型時,判斷其ASCII碼在0~127之間即為通用ASCII字元,如果是負值,則算是擴展ASCII字元,一般漢字都是用連續兩三個擴展ASCII字元表示的,所以判斷ASCII是否小於0即可;如果字元串類型是無符號的unsigned char則可以判斷漢字字元大於127。

閱讀全文

與python如何判斷字元串中含有中文相關的資料

熱點內容
優信二手車解壓後過戶 瀏覽:61
Windows常用c編譯器 瀏覽:778
關於改善國家網路安全的行政命令 瀏覽:833
安卓如何下載網易荒野pc服 瀏覽:654
javainetaddress 瀏覽:104
蘋果4s固件下載完了怎麼解壓 瀏覽:1002
命令zpa 瀏覽:285
python編譯器小程序 瀏覽:944
在app上看視頻怎麼光線調暗 瀏覽:540
可以中文解壓的解壓軟體 瀏覽:592
安卓卸載組件應用怎麼安裝 瀏覽:913
使用面向對象編程的方式 瀏覽:339
程序員項目經理的年終總結範文 瀏覽:929
內衣的加密設計用來幹嘛的 瀏覽:432
淮安數據加密 瀏覽:292
魔高一丈指標源碼 瀏覽:982
松下php研究所 瀏覽:168
c回調java 瀏覽:399
夢幻端游長安地圖互通源碼 瀏覽:745
電腦本地文件如何上傳伺服器 瀏覽:313