【问题标题】:Loading UTF-8 file in Python 3 using numpy.genfromtxt()使用 numpy.genfromtxt() 在 Python 3 中加载 UTF-8 文件
【发布时间】: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


【解决方案1】:

当我尝试重现您的代码时,我遇到了日期转换问题:

Out[405]: b'1-Apr-15'
In [406]: str2date(_)
---------------------------------------------------------------------------
...
----> 1 str2date = lambda x: datetime.strptime(x, '%d-%b-%y').strftime('%Y-%m-%d')

TypeError: strptime() argument 1 must be str, not bytes

如果我添加decode:

def foo(x):
    return str2date(x.decode())

转换器处理genfromtxt坚持提供的字节串。

In [410]: data = np.genfromtxt('stack47619155.txt', dtype="S9,f8,f8,f8,f8,f8", 
     ...: delimiter=',', names=True,  missing_values='-', converters={0: foo})
In [411]: data
Out[411]: 
array([(b'2015-04-0',  108.33,  108.66,  108.33,  108.66,  290.),
       (b'2015-04-0',     nan,  108.66,     nan,  108.66,  290.),
       (b'2015-04-0',  108.33,  108.66,  108.33,  108.66,   nan)],
      dtype=[('Date', 'S9'), ('Open', '<f8'), ('High', '<f8'), ('Low', '<f8'), ('Close', '<f8'), ('Volume', '<f8')])
In [412]: data = np.genfromtxt('stack47619155.txt', dtype="U9,f8,f8,f8,f8,f8", 
     ...: delimiter=',', names=True,  missing_values='-', converters={0: foo})
In [413]: data
Out[413]: 
array([('2015-04-0',  108.33,  108.66,  108.33,  108.66,  290.),
       ('2015-04-0',     nan,  108.66,     nan,  108.66,  290.),
       ('2015-04-0',  108.33,  108.66,  108.33,  108.66,   nan)],
      dtype=[('Date', '<U9'), ('Open', '<f8'), ('High', '<f8'), ('Low', '<f8'), ('Close', '<f8'), ('Volume', '<f8')])

这是一个不同的错误,所以我可能使用了不同的- 作为缺失字段标记。

您在几年前发现我的帖子,在转换器中带有 decode

Loading UTF-8 file in Python 3 using numpy.genfromtxt

【讨论】:

  • decode() 在 python2 中是必需的。对于 python3,它会引发错误(在我的系统上)。没有decode() 的转换本身在print(str2date('1-Apr-15')) 处运行良好。
  • print(str2date(b'1-Apr-15')) 怎么样?
  • 那我需要decode,是的。但是genfromtxt 仍然因ascii 问题而失败。
猜你喜欢
  • 2016-01-05
  • 2015-04-12
  • 1970-01-01
  • 2018-07-18
  • 2014-04-18
  • 2017-01-27
相关资源
最近更新 更多