Python:检查字符串是否包含中文字符?

编程入门 行业动态 更新时间:2024-10-14 22:19:40
本文介绍了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)[u'\u4e0a\u6d77', u'\u8679\u6865']

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'\u4e0a\u6d77', u'\u8679\u6865']

更多推荐

Python:检查字符串是否包含中文字符?

本文发布于:2023-10-27 10:31:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533107.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   中文   字符   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!