Where is the syntax for the dict() constructor ?!
I'm simply trying to read a CSV into a dictionary.
(if it matters, it's ZIP codes and time zones, i.e.,
35983,CT
39161,CT
47240,EST
Apparently the way to do this is:
import csv
dictZipZones = {}
reader = csv.reader(open("some.csv", "rb"))
for row in reader:
# Add the row to the dictionary
But how to do this?
Apparently there is no dict.append() nor dict.add()
But what is there? I see vague references to "the dict() constructor"
and some examples, and news that it has been recently improved. But
where is the full, current documentation for the dict() constructor?
Frustrated,
Captain Poutine
--
http://mail.python.org/mailman/listinfo/python-list
Re: Where is the syntax for the dict() constructor ?!
Neil Cerutti wrote:
> On 2007-07-05, Captain Poutine <[EMAIL PROTECTED]> wrote:
>> I'm simply trying to read a CSV into a dictionary.
>>
>> (if it matters, it's ZIP codes and time zones, i.e.,
>> 35983,CT
>> 39161,CT
>> 47240,EST
>>
>>
>>
>> Apparently the way to do this is:
>>
>> import csv
>>
>> dictZipZones = {}
>>
>> reader = csv.reader(open("some.csv", "rb"))
>> for row in reader:
>> # Add the row to the dictionary
>
> In addition to Chris's answer, the csv module can read and write
> dictionaries directly. Look up csv.DictReader and csv.DictWriter.
>
Yes, thanks. I was happy when I saw it at
http://www.python.org/doc/2.4/lib/node615.html
"Reader objects (DictReader instances and objects returned by the
reader() function) have the following public methods:
next( )
Return the next row of the reader's iterable object as a list,
parsed according to the current dialect."
But that's not enough information for me to use. Also, the doc says
basically "csv has dialects," but doesn't even enumerate them. Where is
the real documentation?
Also, when I do a print of row, it comes out as:
['12345', 'ET']
But there are no quotes around the number in the file. Why is Python
making it a string?
--
http://mail.python.org/mailman/listinfo/python-list
Re: Where is the syntax for the dict() constructor ?!
Peter Otten wrote:
> Neil Cerutti wrote:
>
>> On 2007-07-05, Captain Poutine <[EMAIL PROTECTED]> wrote:
>>> I'm simply trying to read a CSV into a dictionary.
>>>
>>> (if it matters, it's ZIP codes and time zones, i.e.,
>>> 35983,CT
>>> 39161,CT
>>> 47240,EST
>>>
>>>
>>>
>>> Apparently the way to do this is:
>>>
>>> import csv
>>>
>>> dictZipZones = {}
>>>
>>> reader = csv.reader(open("some.csv", "rb"))
>>> for row in reader:
>>> # Add the row to the dictionary
>> In addition to Chris's answer, the csv module can read and write
>> dictionaries directly. Look up csv.DictReader and csv.DictWriter.
>
> DictReader gives one dict per row, with field names as keys. The OP is more
> likely to want
>
> dict(csv.reader(open("some.csv", "rb")))
>
> which produces a dict that maps ZIP codes to time zones.
>
> Peter
>
Thanks Peter, that basically works, even if I don't understand it.
What does "rb" mean? (read binary?)
Why are the keys turned into strings (they are not quoted in the .csv file)?
--
http://mail.python.org/mailman/listinfo/python-list
