导航:首页 > 编程语言 > python分辨中文英文

python分辨中文英文

发布时间:2022-04-18 23:21:34

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如何判断一个字符是中文还是英文

逐个字符用ord()判断ascii码

a - z : 97 - 122

A - Z : 65 - 90

defis_english_char(ch):
iford(ch)notin(97,122)andord(ch)notin(65,90):
returnFalse
returnTrue

上面函数可以辨别字符是否为英文字符

③ python怎么判断中文字符编码

#!/usr/bin/env python
# -*- coding:GBK -*-

"""汉字处理的工具:
判断unicode是否是汉字,数字,英文,或者其他字符。
全角符号转半角符号。"""

__author__="internetsweeper <[email protected]>"
__date__="2007-08-04"

def is_chinese(uchar):
"""判断一个unicode是否是汉字"""
if uchar >= u'\u4e00' and uchar<=u'\u9fa5':
return True
else:
return False

def is_number(uchar):
"""判断一个unicode是否是数字"""
if uchar >= u'\u0030' and uchar<=u'\u0039':
return True
else:
return False

def is_alphabet(uchar):
"""判断一个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):
"""判断是否非汉字,数字和英文字符"""
if not (is_chinese(uchar) or is_number(uchar) or is_alphabet(uchar)):
return True
else:
return False

def B2Q(uchar):
"""半角转全角"""
inside_code=ord(uchar)
if inside_code<0x0020 or inside_code>0x7e: #不是半角字符就返回原来的字符
return uchar
if inside_code==0x0020: #除了空格其他的全角半角的公式为:半角=全角-0xfee0
inside_code=0x3000
else:
inside_code+=0xfee0
return unichr(inside_code)

def Q2B(uchar):
"""全角转半角"""
inside_code=ord(uchar)
if inside_code==0x3000:
inside_code=0x0020
else:
inside_code-=0xfee0
if inside_code<0x0020 or inside_code>0x7e: #转完之后不是半角字符返回原来的字符
return uchar
return unichr(inside_code)

def stringQ2B(ustring):
"""把字符串全角转半角"""
return "".join([Q2B(uchar) for uchar in ustring])

def uniform(ustring):
"""格式化字符串,完成全角转半角,大写转小写的工作"""
return stringQ2B(ustring).lower()

def string2List(ustring):
"""将ustring按照中文,字母,数字分开"""
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

以上转自http://hi..com/fenghua1893/item/d1a71d5ac47ffdcfd3e10cd1

这个问题是做 MkIV 预处理程序时搞定的,就是把一个混合了中英文混合字串分离为英文与中文的子字串,譬如,将 ”我的 English 学的不好“ 分离为 “我的"、" English ” 与 "学的不好" 三个子字串。
1. 中英文混合字串的统一编码表示中英文混合字串处理最省力的办法就是把它们的编码都转成 Unicode,让一个汉字与一个英文字母的内存位宽都是相等的。这个工作用 Python 来做,比较合适,因为 Python 内码采用的是 Unicode,并且为了支持 Unicode 字串的操作,Python 做了一个 Unicode 内建模块,把 string 对象的全部方法重新实现了一遍,另外提供了 Codecs 对象,解决各种编码类型的字符串解码与编码问题。
譬如下面的 Python 代码,可实现 UTF-8 编码的中英文混合字串向 Unicode 编码的转换:# -*-
coding:utf-8 -*-
a = "我的 English 学的不好"
print type(a),len (a), a
b = unicode (a, "utf-8")
print type(b), len (b), b字符串 a 是 utf-8 编码,使用 python 的内建对象 unicode 可将其转换为 Unicode 编码的字符串 b。上述代码执行后的输出结果如下所示,比较字串 a 与字串 b 的长度,显然 len (b) 的输出结果是合理的。<type 'str'> 27 我的 English 学的不好
<type 'unicode'> 15 我的 English 学的不好要注意的一个问题是 Unicode 虽然号称是“统一码”,不过也是存在着两种形式,即:
UCS-2:为 16 位码,具有 2^16 = 65536 个码位; UCS-4:为 32 位码,目前的规定是其首字节的首位为 0,因此具有 2^31 = 2147483648 个码位,不过现在的只使用了 0x00000000 - 0x0010FFFF 之间的码位,共 1114112 个。
使用Python sys 模块提供的一个变量 maxunicode 的值可以判断当前 Python 所使用的 Unicode 类型是 UCS-2 的还是 UCS-4 的。import sys
print sys.maxunicode若 sys.maxunicode 的值为 1114111,即为 UCS-4;若为 65535,则为 UCS-2。

2. 中英文混合字串的分离一旦中英文字串的编码获得统一,那么对它们进行分裂就是很简单的事情了。首先要为中文字串与英文字串分别准备一个收集器,使用两个空的字串对象即可,譬如 zh_gather 与 en_gather;然后要准备一个列表对象,负责按分离次序存储 zh_gather 与 en_gather 的值。下面这个 Python 函数接受一个中英文混合的 Unicode 字串,并返回存储中英文子字串的列表。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上述代码所实现的功能细节是:对中英文混合字串 zh_en_str 的遍历过程中进行逐字识别,若当前字符为中文,则将其添加到 zh_gather 中;若当前字符为英文,则将其添加到 en_gather 中。zh_status 表示中英文字符的切换状态,当 zh_status 的值发生突变时,就将所收集的中文子字串或英文子字串添加到 zh_en_group 中去。
判断字串 zh_en_str 中是否包含中文字符的条件语句中出现了一个 is_zh () 函数,它的实现如下: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 预处理程序。
对于分离出来的中文子字串与英文子字串,为了使用方便,在将它们存入 zh_en_group 列表时,我对它们分别做了标记,即 mark["zh"] 与 mark["en"]。mark 是一个 dict 对象,其定义如下:mark = {"en":1, "zh":2}如果要对 zh_en_group 中的英文字串或中文字串进行处理时,标记的意义在于快速判定字串是中文的,还是英文的,譬如:for str in zh_en_group:
if str[0] = mark["en"]:
do somthing
else:
do somthing

④ 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就可以了。

⑤ python 判断字符串中是否含有英文

使用isalpha()方法来进行判断。Python isalpha() 方法检测字符串是否只由字母组成。如果字符串至少有一个字符并且所有字符都是字母则返回 True,否则返回 False。

isalpha()方法要检测的字符。它可以是一个有效的字符(被转换为 int 类型),也可以是 EOF(表示无效的字符)。


(5)python分辨中文英文扩展阅读

通常认为只有"abc...xyzABC...XYZ"才是字母,其实这是不对的。字母并不是固定的,不同的语言文化可能会包含不同的字母,例如在“简体中文”环境中,西里尔文БГЁ、希腊文ΣΩΔΨΦ(数学物理公式中常用希腊字母)等都将成为字母。

可以通过 setlocale() 函数改变程序的地域设置,让程序使用不同的字符集,从而支持不同的语言文化。一个字母要么是小写字母,要么是大写字母;并且一个小写字母必定对应一个大写字母,反之亦然。这种说法虽然适用于默认的地域设置(默认为"C"),但是并不一定适用于其它的地域设置。

⑥ python如何判断字符是中文还是英文字母

判断如下:

1、逐个字符用ord()判断ascii码:a - z : 97 - 122,A - Z : 65 - 90。

2、def is_english_char(ch):if ord(ch) not in (97,122) and ord(ch) not in (65,90):return False,return True。

Python在设计上坚持了清晰划一的风格,这使得Python成为一门易读、易维护,并且被大量用户所欢迎的、用途广泛的语言。

(6)python分辨中文英文扩展阅读:

Python的控制语句:

1、if语句,当条件成立时运行语句块。经常与else, elif(相当于else if) 配合使用。

2、for语句,遍历列表、字符串、字典、集合等迭代器,依次处理迭代器中的每个元素。

3、while语句,当条件为真时,循环运行语句块。

4、try语句,与except,finally配合使用处理在程序运行中出现的异常情况。

5、class语句,用于定义类型。

⑦ 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

⑧ python 如何利用字符串索引来分含有中英文的字符串,字符串如“府绸 poplin 府绸绉 crepe poplin......”

python 3 这其实是扫描的中英文字典,所以要一一对应,最好可以保存到excel里面。

⑨ python 判断字符串是否是一段中文

嘻嘻,渣度机器人队再得队1分。13级巨神又遭调戏。

阅读全文

与python分辨中文英文相关的资料

热点内容
压缩包解码器下载 浏览:130
爱旅行的预备程序员 浏览:111
安卓qq浏览器怎么转换到ios 浏览:292
不同编译器的库可以调用吗 浏览:455
灰度信托基金加密 浏览:421
宿迁程序员兼职网上接单 浏览:924
电视编译器怎么设置 浏览:276
手机如何解压汉字密码的压缩包 浏览:701
为什么很多程序员爱用vim 浏览:828
安卓手机怎么连接宝华韦健音响 浏览:555
12星座制作解压球 浏览:867
java调用oracle数据 浏览:827
怎么在服务器上上传小程序源码 浏览:304
空中加油通达信指标公式源码 浏览:38
分卷解压只解压了一部分 浏览:760
php网站自动登录 浏览:705
合肥凌达压缩机招聘 浏览:965
怎么找到文件夹的图标 浏览:237
linuxc编程pdf百度云 浏览:192
会计pdf下载 浏览:835