1

我想加载一个具有以下格式的 txt:

20-Sep-13,178.90,185.83,178.56,183.39,13401689
19-Sep-13,170.80,180.47,169.08,177.92,15594568
18-Sep-13,167.07,167.45,164.20,166.22,5439615
17-Sep-13,165.08,168.42,163.36,166.23,5500719

因此,我使用以下 Python 行:

date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile, delimiter=',', unpack=True, converters={0: mdates.strpdate2num('%d-%b-%y')})

但我收到以下错误:

failed main loop time data '\xef\xbb\xbf20-Sep-13' does not match format '%d-%b-%y'

有谁知道,我做错了什么?

谢谢,麦基

4

1 回答 1

3

文本文件包含 UTF-8 BOM 字符。numpy.loadtxt不接受encoding,但您可以传递 iterable 而不是文件名。

尝试以下操作:

stockFile = '....'


import numpy as np
import matplotlib.dates as mdates
import codecs

with codecs.open(stockFile, encoding='utf-8-sig') as f:
    date, closep, highp, lowp, openp, volume = np.loadtxt(f, delimiter=',', unpack=True, converters={0: mdates.strpdate2num('%d-%b-%y')})
于 2013-09-23T12:38:26.687 回答