Best way to create a Dictionary from file of Key-Value pairs?

Equis Uno ir4u4 at yahoo.com
Mon Mar 1 07:08:54 CET 2004


I must say, CLP rocks.

I added some syntax to convert the key from string to a time data
type:

import time
# Convert the values to float and the keys to time
myfile = file('csvData.txt')
myfile.readline() # skip "date,value" line
datevaluemap = dict([( time.strptime(key, '%d-%b-%y'), float(value))
for (key, value) in csv.reader(myfile)])
print datevaluemap

output:

{(2004, 2, 24, 0, 0, 0, 1, 55, -1): 36.299999999999997, (2004, 2, 19,
0, 0, 0, 3, 50, -1): 37.869999999999997, (2004, 2, 26, 0, 0, 0, 3, 57,
-1): 36.469999999999999, (2004, 2, 20, 0, 0, 0, 4, 51, -1): 37.0,
(2004, 2, 23, 0, 0, 0, 0, 54, -1): 37.0, (2004, 2, 25, 0, 0, 0, 2, 56,
-1): 36.43}

I'm way happy!

-moi



More information about the Python-list mailing list