python-3.x - 'cp866' 的 Python 编码/解码错误
问题描述
6.5,我试图从 CSV 文件中提取一些信息,但文件是用俄语编写的,所以我需要使用 'cp866' 来解码。但是,我无法获得正确的输出。
这是我使用的代码:
def printcsv():
with open('vocabulary.csv',newline='') as f:
reader = csv.reader(f)
for row in reader:
#store in array
print(row.decode('cp866'))
这是我得到的错误:
"/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/encodings/ascii.py", line 26, in decode
return codecs.ascii_decode(input, self.errors)[0]
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa7 in position 0: ordinal not in range(128)
解决方案
哎呀,这不是读取编码的 csv 文件的正确方法。这是您尝试执行的操作:
with open('vocabulary.csv',newline='') as f: # open the file with default system encoding
reader = csv.reader(f) # declare a reader on it
for row in reader: # here comes the problem
我假设您的系统使用 ASCII 作为默认编码。因此,当阅读器尝试加载一行时,会从文件中读取一行(字节)并将其解码为默认 ascii 编码的字符串。
无论如何,row
是一个列表而不是一个字符串,所以row.decode
如果你到达那一行就会引发错误。
如果在打开文件时指定文件编码的正确方法:
def printcsv():
with open('vocabulary.csv',newline='', encoding='cp866') as f:
reader = csv.reader(f)
for row in reader:
#store in array
但我不确定
print(row)
根据 使用的编码sys.stdout
,您可能必须对数组中的每个字符串进行显式编码:
print([ field.encode(encoding) for field in row ])
推荐阅读
- r - 是否有计算生存概率的威布尔估计的函数
- python - 当有打印指令时,线程没有做他们的工作
- javascript - 如何将 Arduino 代码在线编译成 .hex 文件(与 Arduino 一起闪烁)?
- r - 如何编写具有固定和随机效应的线性模型
- python - 使用 openpyxl 添加注释
- powershell - Powershell,我确实输入了一个列表收集数据并将整个列表输出到一个 CSV
- formatjs - 如何给 intl-messageformat-parser 起别名?
- django - 如何用标准 django 管理视图中的操作列替换 django-simple-history 视图中的评论列?
- python - 匹配几个可变参数
- javascript - 如何使有效载荷响应返回数据为不同的格式?