0

In PSET6 DNA, I have to read the CSV file.

I use

with open("P:\Code\Python\dna\databases\small.csv") as csvfile:   #open on my PC
    dna_db = csv.DictReader(csvfile)
    for row in dna_db:
        print(row)

The dna_db seeems ok:

{'name': 'Alice', 'AGATC': '2', 'AATG': '8', 'TATC': '3'}

{'name': 'Bob', 'AGATC': '4', 'AATG': '1', 'TATC': '5'}

{'name': 'Charlie', 'AGATC': '3', 'AATG': '2', 'TATC': '5'}

I can't find how to exploit the new dictionary (dna_db). I thought it's was a nested dictionary, but Maybe it's just 3 dictionaries.

I've try with:

print(dna_db[0][0]) => TypeError: 'DictReader' object is not subscriptable

print(dna_db[0]['name']) => TypeError: 'DictReader' object is not subscriptable

Someone could help me with the code or the logic to extract 1 value? For exemple 'Bob' or the '1' ?

Thanks

1

A DictReader object (instance) has the public methods list in this doc. It is not a python Built-in type, and cannot be used as such. One approach would be to store the rows in a python collection (eg list, dict).

| improve this answer | |
  • Ok thanks a lot. The working print was a lure :). I have to find how to fill a list with it. – saquiel Jan 16 at 19:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .