【发布时间】:2018-05-17 01:54:58
【问题描述】:
我正在尝试在 python3 中使用 numpy.genfromtxt() 读取包含不同日期数据的文件。该文件基本上看起来像
Date,Open,High,Low,Close,Volume
1-Apr-15,108.33,108.66,108.33,108.66,290
但可能包含标记为-的缺失值。
以下代码在python2中运行良好
str2date = lambda x: datetime.strptime(x, '%d-%b-%y').strftime('%Y-%m-%d')
data = np.genfromtxt('test.dat', dtype="S9,f8,f8,f8,f8,f8", delimiter=',', names=True, missing_values='-', converters={0: str2date})
但在 python3 中失败了
UnicodeDecodeError: 'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
locale.getpreferredencoding(False) 返回 UTF-8 作为默认编码,建议的解决方案是通过设置输入流的编码来建议例如 here 有点棘手。我也尝试设置encoding of the terminal,但没有成功。我还必须承认,我在 this answer 中看不到我的问题的解决方案,因为文件中没有包含特殊字符——或者至少我没有看到它们。
如何在不回到 python2 的情况下解决这个问题?
【问题讨论】:
-
似乎 genfromtxt 出于未定义的原因进入 ascii 模式....您尝试过 genfromtxt(open('test.dat', encoding='utf-8'), ... 吗?更高效,pandas.read_csv?
-
genfromtxt(open('test.dat', encoding='utf-8'))抱怨提供字节而不是字符串。但是 pandas 就像一个魅力。谢谢 :)。如果你把它放在一个答案中,我会接受它。 -
genfromtxt以二进制模式打开文件,并使用字节串 (Py3)。 stackoverflow.com/questions/33001373/… 中的converters解决方案没有帮助? -
我理解这是解决有问题的文件名的方法。我没有。
标签: python-3.x numpy utf-8