gpt4 book ai didi

python - 在python中,提取非英语单词

转载 作者:太空宇宙 更新时间:2023-11-03 18:28:23 24 4
gpt4 key购买 nike

我有一个包含英语字符和其他语言字符的文本文件。使用下面的代码,我想从这个文件中提取一些非英语单词特别是韩语(UTF-8 中的 Unicode 范围从 AC00 到 D7AF)

有什么方法可以在这段代码中做到这一点吗?

我还需要做其他事情吗?

....
text = f.read()
words = re.findall(r'\w+', dataString)
f.close()
....

最佳答案

使用大写 \W = 匹配字母数字字符,不包括 _

>>> re.findall('[\W]+', u"# @, --►(Q1)-grijesh--b----►((Qf)), ");
[u'# @, --\u25ba(', u')-', u'--', u'----\u25ba((', u')), ']

来自:Unicode HOWTO?要读取 unicode 文本文件,请使用:

import codecs
f = codecs.open('unicode.rst', encoding='utf-8')
for l in f:
# regex code here

我有一个文件:

:~$ cat file
# @, --►(Q1)-grijesh--b----►((Qf)),

从Python读取它:

>>> import re
>>> import codecs
>>> f = codecs.open('file', encoding='utf-8')
>>> for l in f:
... print re.findall('[\W]+', l)
...
[u'# @, --\u25ba(', u')-', u'--', u'----\u25ba((', u')),\n']
>>>

要阅读字母单词,请尝试

>>> f = codecs.open('file', encoding='utf-8')
>>> for l in f:
... print re.findall('[^\W]+', l)
...
[u'Q1', u'grijesh', u'b', u'Qf']

注意:小\w 匹配字母数字字符,包括 _

关于python - 在python中,提取非英语单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22790234/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com