当前位置: 首页 > 工具软件 > OpenP2M > 使用案例 >

python date2num_文本文件mdates.strpdate2num

公西嘉玉
2023-12-01

我在使用numpy loadtxt转换器时总是遇到一个错误。在

非常感谢你的帮助import numpy as np

import time

import datetime

import matplotlib.pyplot as plt

import matplotlib.ticker as mticker

import matplotlib.dates as mdates

from matplotlib.finance import candlestick

from matplotlib.dates import strpdate2num

import urllib2

## global variables

eachStock = 'AAPL','GOOG','MSFT','AMZN','CMG'

for stock in eachStock:

stockFile = stock+'.txt'

date, closep, highp, lowp, openp, volume = np.loadtxt(eachStock, delimiter=',', unpack=True,

converters={ 0: mdates.strpdate2num('%Y%m%d')})

dFrame = Series(closep)

这是我文本文件中的第一行

^{pr2}$

这是我一直得到的错误Traceback (most recent call last):

File "C:\Users\antoniozeus\Desktop\BuyAndHold.py", line 27, in

converters={ 0: mdates.strpdate2num('%Y%m%d')})

File "C:\Python27\lib\site-packages\numpy\lib\npyio.py", line 796, in loadtxt

items = [conv(val) for (conv, val) in zip(converters, vals)]

File "C:\Python27\lib\site-packages\matplotlib\dates.py", line 233, in __call__

return date2num(datetime.datetime(*time.strptime(s, self.fmt)[:6]))

File "C:\Python27\lib\_strptime.py", line 454, in _strptime_time

return _strptime(data_string, format)[0]

File "C:\Python27\lib\_strptime.py", line 325, in _strptime

(data_string, format))

ValueError: time data 'AAPL' does not match format '%Y%m%d'

 类似资料: