I try to read some data from many .csv files and store them into arrays to plot them. To be specific there are two arrays. In the first one, I throw in the date and time information. In the original data, this is in the format of
Date=2009/07/29 15:28:46
the second array is in the form
SpectrumMax=1.440973
when plotting these values, the program is terminated with message:
....
ValueError: invalid literal for float(): 2009/07/29 15:28:46
When printing the array, where the datetime is stored, it shows:
...
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']
Compared to the initial data, the dates changed their separators from slash to dot. Why is this and how can I suppress this obviously automatic conversion?
The original code is:
#!/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()
The big question is, how to plot the data with date/time on x-axis?