python判断字符串英文和中文(python 判断是不是中文字)

本文目录
- python 判断是不是中文字
- python 判断是否含有数字,英文字符和汉字
- python字符串一个是汉字,一个是字母怎么对比
- 用Python从键盘输入一个有中文和英文的字符串,编程分别输出中文和英文,并统
- Python判断字符串中是否有中文字符
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
***隐藏网址***
》》》 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 判断是否含有数字,英文字符和汉字
#-*- coding:utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding(’utf8’)
def check_contain_chinese(check_str):
for ch in check_str.decode(’utf-8’):
if u’\u4e00’ 《= ch 《= u’\u9fff’:
return True
return False
def check_contain_digit(check_str):
for ch in check_str:
if ch.isdigit():
return True
return False
def check_contain_English(check_str):
for ch in check_str:
if ch.isalpha():
return True
return False
if __name__ == "__main__":
print check_contain_chinese(’中国’)
print check_contain_digit(’1xx’)
print check_contain_English(’xx中国ch’)
python字符串一个是汉字,一个是字母怎么对比
两个字符串长度不相等。比如 wuhan 和 wuhana
2. 两个字符串不仅长度相等,而且对应位置上的字符完全一致(区分大小写)。比如 Wuhan 和Wuhan
3. 两个字符串长度相等,
用Python从键盘输入一个有中文和英文的字符串,编程分别输出中文和英文,并统
from string import ascii_letters
x=input("输入字符串:")
hz=
zm=
for xx in x:
if xx in ():
hz.append(xx)
print(f"汉字:{xx}")
elif xx in ascii_letters:
zm.append(xx)
print(f"字母:{xx}")
print()
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

更多文章:
在from子句中可以出现(如何在from 子句中嵌套查询下面的语句在access中出错!)
2026年10月11日 05:20
countif函数统计个数怎么用(countif函数怎么用 详解Excel中countif函数的使用方法)
2026年10月11日 03:30
正则匹配数字之前的字符(正则表达式如何匹配前面是数字、中间是“/”、后面也是数字,就像2/3专业的模式)
2026年10月11日 03:00
orlnsertbootmediinselected(我电脑开机显示这个是什么意思or insert boot media in select)
2026年10月10日 23:00
display的用法(display是什么意思 详解display的含义和用法)
2026年10月10日 22:00






