CSV Dictionary

Skip Montanaro skip.montanaro at gmail.com
Mon Dec 29 10:47:23 EST 2014


On Mon, Dec 29, 2014 at 9:35 AM, JC <chalao.adda at gmail.com> wrote:
> How could I get the all the records?

This should work:

with open('x.csv','rb') as f:
    rdr = csv.DictReader(f,delimiter=',')
    rows = list(rdr)

You will be left with a list of dictionaries, one dict per row, keyed
by the values in the first row:

>>> import csv
>>> with open('x.csv','rb') as f:
...     rdr = csv.DictReader(f,delimiter=',')
...     rows = list(rdr)
...
>>> import pprint
>>> pprint.pprint(rows)
[{'id': '1', 'name': 'ABC', 'password': '1234', 'userid': 'def at ghi'},
 {'id': '2', 'name': 'DEF', 'password': 'asdf', 'userid': 'ghi at jkl'},
 {'id': '3', 'name': 'GHI', 'password': 'zxcv', 'userid': 'jkl at mno'}]

Skip



More information about the Python-list mailing list