我一直在关注绘制股票图表的教程,并遇到了一个让我难过的语法错误。我的配置是Windows XP,Python 2.7 Anaconda发行版。
我的文本文件看起来像这样,没有标题:date,close,high,low,open,volume:
20130128,449.8300,453.2100,435.8600,437.8300,28054200 20130129,458.2700,460.2000,452.1200,458.5000,20398500 20130130,456.8300,462.6000,454.5000,457.0000,14898400 20130131,455.4900,459.2800,454.9800,456.9800,11404800 20130201,453.6200,459.4800,448.3500,459.1100,19267300 20130204,442.3200,455.9400,442.0000,453.9100,17039900
以下是代码:
import time
import datetime
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.ticker as mticker
import matplotlib.dates as mdates
eachStock = 'TSLA','AAPL'
def graphData(stock):
try:
stockFile = stock+'.txt'
date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile, delimiter=",", unpack=True, converters=(0: mdates.strpdate2num('%Y%m%d')))
fig = plt.figure()
ax1 = plt.subplot(1,1,1)
ax1.plot(date, openp)
ax1.plot(date, highp)
ax1.plot(date, lowp)
ax1.plot(date, closep)
plt.show()
except Exception, e:
print 'failed main loop',str(e)
for stock in eachStock:
graphData(stock)
time.sleep(300)
我一直收到以下错误:
date, closep, highp, lowp, openp, volume = np.loadtxt(stockFile, delimiter=",", unpack=True, converters=(0: mdates.strpdate2num('%Y%m%d')))
^
SyntaxError: invalid syntax
我很难过,因为我相信我已经复制了一个适用于其他人的代码功能,但由于某种原因,这对我来说是一个错误。谢谢你的帮助!
答案 0 :(得分:1)
而不是
(0: mdates.strpdate2num('%Y%m%d'))
试
{0: mdates.strpdate2num('%Y%m%d')}
至少应该删除语法错误。 {:}
都是字典定义的一部分。