Best way to create a Dictionary from file of Key-Value pairs?
vincent wehren
vincent at visualtrans.de
Sun Feb 29 16:03:30 EST 2004
"Equis Uno" <ir4u4 at yahoo.com> schrieb im Newsbeitrag
news:692feddd.0402291222.6f2d619e at posting.google.com...
| Hi,
|
| Assume I'm given a 100k file full of key-value pairs:
|
| date,value
| 26-Feb-04,36.47
| 25-Feb-04,36.43
| 24-Feb-04,36.30
| 23-Feb-04,37.00
| 20-Feb-04,37.00
| 19-Feb-04,37.87
|
| What is the best way to copy this data into a Dictionary object?
If it suffices ro generate a "string-to-string mapping", you could do:
>>> d = {}
>>> for line in file("infile"):
k, v = line.rstrip().split(",")
d[k] = v
This will give you:
{'20-Feb-04': '37.00', '19-Feb-04': '37.87', '26-Feb-04': '36.47',
'23-Feb-04': '37.00', '25-Feb-04': '36.43', '24-Feb-04': '36.30'}
HTH,
Vincent Wehren
|
| -moi
More information about the Python-list
mailing list