gpt4 book ai didi

python - 如何使用 Python 读取 utf-8 编码的文本文件

转载 作者:太空狗 更新时间:2023-10-30 00:38:58 25 4
gpt4 key购买 nike

我需要分析泰米尔语(utf-8 编码)的文本文件。我在 IDLE 接口(interface)上使用 Python 的 nltk 包。当我尝试读取界面上的文本文件时,这是我得到的错误。我该如何避免这种情况?

corpus = open('C:\\Users\\Customer\\Desktop\\DISSERTATION\\ettuthokai.txt').read()

Traceback (most recent call last):
File "<pyshell#2>", line 1, in <module>
corpus = open('C:\\Users\\Customer\\Desktop\\DISSERTATION\\ettuthokai.txt').read()
File "C:\Users\Customer\AppData\Local\Programs\Python\Python35-32\lib\encodings\cp1252.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8d in position 33: character maps to <undefined>

最佳答案

由于您使用的是 Python 3,只需将 encoding 参数添加到 open() 即可:

corpus = open(
r"C:\Users\Customer\Desktop\DISSERTATION\ettuthokai.txt", encoding="utf-8"
).read()

关于python - 如何使用 Python 读取 utf-8 编码的文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40918503/

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