2011年09月19日 星期一 15:57
python 读取缓存文件夹下的csv 出错了,具体代码如下:
import numpy
from matplotlib.mlab import csv2rec
from pylab import figure, show
import matplotlib.cbook as cbook
from matplotlib.ticker import Formatter
datafile = 'C:\.matplotlib\sample_data\601988.csv'
r = csv2rec(datafile)[-40:]
class MyFormatter(Formatter):
def __init__(self, dates, fmt='%Y-%m-%d'):
self.dates = dates
self.fmt = fmt
def __call__(self, x, pos=0):
'Return the label for time x at position pos'
ind = int(round(x))
print x
if ind>=len(self.dates) or ind<0: return ''
return self.dates[ind].strftime(self.fmt)
formatter = MyFormatter(r.date)
fig = figure()
ax = fig.add_subplot(111)
ax.xaxis.set_major_formatter(formatter)
ax.plot(numpy.arange(len(r)), r.close, 'o-')
fig.autofmt_xdate()
show()
运行出现raceback (most recent call last):
File "F:\practice-py\cbook2.py", line 8, in <module>
r = csv2rec(datafile)[-40:]
File "F:\python\lib\site-packages\matplotlib\mlab.py", line 2140, in csv2rec
fh = cbook.to_filehandle(fname)
File "F:\python\lib\site-packages\matplotlib\cbook.py", line 434, in to_filehandle
fh = file(fname, flag)
IOError: [Errno 2] No such file or directory: 'C:\\.matplotlib\\sample_data.csv'
我的文件是存储在C:\.matplotlib\sample_data下的
2011年09月19日 星期一 16:09
用datafile = r'C:\.matplotlib\sample_data\601988.csv'
否则字符串中的\是用来转义的,会导致路径不正确。
Zeuux © 2024
京ICP备05028076号