我尝试从许多.csv文件中读取一些数据并将它们存储到数组中以绘制它们。具体来说有两个数组。在第一个中,我会输入日期和时间信息。在原始数据中,格式为
Date=2009/07/29 15:28:46
第二个数组的格式为
SpectrumMax=1.440973
绘制这些值时,程序将以消息终止:
....
ValueError: invalid literal for float(): 2009/07/29 15:28:46
打印存储日期时间的数组时,显示:
...
20:32:53', '2013
.09.05 06:46:07', '2013.09.06 08:56:47', '2013.09.09 06:44:08', '2013.09.10 21:2
0:59', '2013.09.12 18:32:34', '2013.09.16 16:13:37', '2013.09.18 06:35:41', '201
3.09.20 06:34:03', '2013.09.23 07:44:46', '2013.09.26 11:08:33', '2013.09.26 12:
36:39', '2013.09.26 13:36:59']
与初始数据相比,日期将其分隔符从斜线更改为点。为什么会这样,我怎么能抑制这种明显的自动转换?
原始代码是:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from pylab import *
import csv
import datetime as DT
import numpy as np
mydir = ('C:\\...\\RickPython\\Background')
f = os.listdir(mydir)
data = [[0],[1]]
os.chdir(mydir)
files = os.listdir(mydir)
for filename in files:
ifile = open(filename, "rb")
#print filename
#ifile = open('Bkg_20090729152846.csv', "rb")
reader = csv.reader(ifile, delimiter='=')
#is_in_csv = False
#search_string = 'Date=2009/07/29 15:28:46'
for num, row in enumerate(reader):
if 'Date' in row:
#is_in_csv = True
#print('Is Date in csv ?'), is_in_csv
data[0].append(row[1])
#print row
if 'SpectrumMax' in row:
#is_in_csv = True
#print('Is SpectrumMax in csv ?'), is_in_csv
data[1].append(float(row[1]))
#print num, row
#print('Line number %s and Row %s')% (num, row)
#print data
#print num
ifile.close()
x = data[0]
y = data[1]
plot_date(x, y, 'r+')
show()
最大的问题是,如何在x轴上用日期/时间绘制数据?