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

后端 未结 4 1628
长发绾君心
长发绾君心 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:42

    you can simply use pandas:

    import csv
    import pandas as pd
    
    data_dict = {}
    with open(argv[1]) as data_file:
        reader = csv.DictReader(data_file)
        df = pd.DataFrame(reader)
        df = df.set_index('name') # set name column as index
        data_dict = df.transpose().to_dict() # transpose to make dict with indexes
       
    print(data_dict)
    

提交回复
热议问题