How can I loop through this dictionary instead of hardcoding the keys

后端 未结 4 1586
长发绾君心
长发绾君心 2021-01-21 18:47

So far, I have this code (from cs50/pset6/DNA):

import csv

data_dict = {}
with open(argv[1]) as data_file:
    reader = csv.DictReader(data_file)
    for record          


        
4条回答
  •  礼貌的吻别
    2021-01-21 19:22

    You are on the right track using csv.DictReader.

    import csv
    from pprint import pprint
    
    data_dict = {}
    
    with open('fasta.csv', 'r') as f:
        reader = csv.DictReader(f)
    
        for record in reader:
            name = record.pop('name')
            data_dict[name] = record
    
    pprint(data_dict)
    

    Prints

    {'Alice': {'AATG': '8', 'AGATC': '2', 'TATC': '3'},
     'Bob': {'AATG': '1', 'AGATC': '4', 'TATC': '5'},
     'Charlie': {'AATG': '2', 'AGATC': '3', 'TATC': '5'}}
    

提交回复
热议问题