I'm using pygtk and glade (python 2.7 32bit interpreter is the most recent it supports) to make an application for a research department, and that requires grabbing a bunch of data from a weather station's csv output and trying to shelve that data in a dictionary for permanent local storage. However, I can't seem to crawl over the wall of storing the date and time information into a datetime object. No matter how many different ways I try to grab the date and time field (represented as a string ex:"11/13/2012 17:43") and put it into a datetime object, I get the same annoying error:
ValueError: time data '' does not match format '%m/%d/%Y %H:%M'
Now for the code in question, it is an event for when a button is clicked on the UI:
def Bupload_clicked(self,widget):
#Create a file chooser window, with default buttons
chooser = gtk.FileChooserDialog("Open . .", None, gtk.FILE_CHOOSER_ACTION_OPEN,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK))
response = chooser.run()
if response == gtk.RESPONSE_OK:
with open(chooser.get_filename(), 'rb') as csvfile:
reader = csv.reader(csvfile, delimiter='\t', quoting=csv.QUOTE_NONE)
try:
for row in reader: #loop through rows of data
#here is the line of code in question:
print repr(datetime.datetime(*time.strptime(row[1], "%m/%d/%Y %H:%M") [:5]))
#rest of code doesn't work until this issue is fixed
except csv.Error as e:
sys.exit('file %s, line %d: %s' % (chooser.get_filename(), reader.line_num, e))
chooser.destroy()
I have tried to accomplish this task in quite a few different ways, using similar questions asked all over the web, including switching the '/' for '-', breaking that one line of code into several lines, etc, but nothing seems to fix the issue.
Also, a side question before you think too hard: The python documentation for strptime() says it looks for [01,12] for %m, [00,24] for %H, [01,31] for %d, etc. But the weather station's CSV file leaves out leading 0's. (ex: "2/1/2013 0:43") would this be causing the errors?
Raw data from CSV file (first 5 lines):
1 11/13/2012 17:43 0 -0.2039 0 43.443 40.2 9.4 0 2.82 70.2 4.375
2 11/13/2012 18:43 0 -0.2039 0 36.651 61.1 0.6 0 0 74.4 4.363
3 11/13/2012 19:43 0 -0.1988 0 32.092 76.1 0.6 0 0.56 74.4 4.357
4 11/13/2012 20:43 0 -0.1988 0 31.591 74.5 0.6 0 1.12 92.7 4.357
5 11/13/2012 21:43 0 -0.1988 0 30.326 82.4 0.6 0 0 223.2 4.351
here is the "in-memory" representation of the list:
#replace the problem line of code with: print row
['1', '11/13/2012 17:43', '0', '-0.2039', '0', '43.443', '40.2', '9.4', '0', '2.82', '70.2', '4.375']
['2', '11/13/2012 18:43', '0', '-0.2039', '0', '36.651', '61.1', '0.6', '0', '0', '74.4', '4.363']
['3', '11/13/2012 19:43', '0', '-0.1988', '0', '32.092', '76.1', '0.6', '0', '0.56', '74.4', '4.357']
['4', '11/13/2012 20:43', '0', '-0.1988', '0', '31.591', '74.5', '0.6', '0', '1.12', '92.7', '4.357']
['5', '11/13/2012 21:43', '0', '-0.1988', '0', '30.326', '82.4', '0.6', '0', '0', '223.2', '4.351']