编码错误使用csv.reader关于非科学编码的io文件对象

2024-06-28 20:44:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试读取一个csv文件,其编码为cp1252,如下所示:

import io
import csv

csvr = csv.reader(io.open('data.csv', encoding='cp1252'))

for row in csvr:
    print row

'的相关内容数据.csv“是吗

^{pr2}$

输出如下

['Curva IV']
['Fecha: 27-Jul-2016 16:22:40']
['Muestra: 1']
Traceback (most recent call last):
  File "D:/sandbox/bla.py", line 347, in <module>
    mist()
  File "D:/sandbox/bla.py", line 343, in mist
    for row in csvr:
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf3' in position 5: ordinal not in range(128)

我完全不明白。很明显,关键的一点是强调“o”。它似乎是csv.reader正在尝试进行转换。异常在print语句之前引发,因此它不是终端编码的问题。你知道这是怎么回事吗?在


Tags: csvinpyioimport编码forreader
1条回答
网友
1楼 · 发布于 2024-06-28 20:44:19

从文件中:

Note

This version of the csv module doesn’t support Unicode input. Also, there are currently some issues regarding ASCII NUL characters. Accordingly, all input should be UTF-8 or printable ASCII to be safe; see the examples in section Examples.

在将输入传递给csv.reader. 在

相关问题 更多 >