Python:检查字符串是否包含汉字? [英] Python: Check if a string contains chinese character?

查看:34
本文介绍了Python:检查字符串是否包含汉字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个字符串可能是这个

ipath="./data/NCDC/上海/虹桥/9705626661750dat.txt"

或者这个

ipath = './data/NCDC/ciampino/6240476818161dat.txt'

我怎么知道第一个字符串包含chinese?

我觉得这个答案可能有帮助:使用 Python 查找字符串中的所有中文文本并正则表达式

但没有成功:

导入重新ipath="./data/NCDC/上海/虹桥/9705626661750dat.txt"re.findall(ur'[u4e00-u9fff]+', ipath) # =>[]

解决方案

匹配的字符串也应该是 unicode

<预><代码>>>>进口重新>>>ipath=u"./data/NCDC/上海/虹桥/9705626661750dat.txt">>>re.findall(r'[u4e00-u9fff]+', ipath)[你'u4e0au6d77',你'u8679u6865']

A string maybe this

ipath= "./data/NCDC/上海/虹桥/9705626661750dat.txt"

or this

ipath = './data/NCDC/ciampino/6240476818161dat.txt'

How do I know the first string contains chinese?

I find this answer maybe helpful: Find all Chinese text in a string using Python and Regex

but it didn't work out:

import re
ipath= "./data/NCDC/上海/虹桥/9705626661750dat.txt"
re.findall(ur'[u4e00-u9fff]+', ipath) # => []

解决方案

The matched string should be unicode as well

>>> import re
>>> ipath= u"./data/NCDC/上海/虹桥/9705626661750dat.txt"
>>> re.findall(r'[u4e00-u9fff]+', ipath)
[u'u4e0au6d77', u'u8679u6865']

这篇关于Python:检查字符串是否包含汉字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆