gpt4 book ai didi

python - 在 python 中为波斯语使用 unicode

转载 作者:行者123 更新时间:2023-11-28 22:43:56 33 4
gpt4 key购买 nike

我正在编写一个脚本来读取语料库文件并查找后缀。由于语料库中有波斯语单词,它是 UTF-8 编码的,但是当我使用波斯语后缀进行搜索时,我没有得到任何结果,另一方面英语结果很好。

from __future__ import unicode_literals
import nltk
import sys


for line in open("corpus.txt"):
for word in line.split():
if word.endswith('ب'):
print (word)

最佳答案

在 Python 3 中,您只需将 encoding=utf-8 传递给 open:

with open("corpus.txt", encoding="utf-8") as fp:
for line in fp:
for word in line.split():
process(word)

在 Python 2 中,您需要执行以下操作:

import codecs
with codecs.open("corpus.txt", encoding="utf-8") as fp:
for line in fp:
for word in line.split():
process(word)

关于python - 在 python 中为波斯语使用 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30104941/

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