简体   繁体   English

如何创建包含信息的嵌套字典。 从 csv 文件

[英]How can I create a nested dictionary containing info. from csv file

I'm working on cs50's pset6, DNA, and I want to read a csv file that looks like this:我正在研究 cs50 的 pset6、DNA,我想读取一个看起来像这样的 csv 文件:

name,AGATC,AATG,TATC
Alice,2,8,3
Bob,4,1,5
Charlie,3,2,5

And what I want to create is a nested dictionary, that would look like this:我想要创建的是一个嵌套字典,它看起来像这样:

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

So I want to use this:所以我想用这个:

with open(argv[1]) as data_file:
    for i in data_file:

(Or another variation) To loop through the csv file and append to the dictionary adding all of the values so that I have a database that I can later access. (或其他变体)循环遍历csv文件和append到字典中,添加所有值,以便我有一个以后可以访问的数据库。

You should use python's csv.DictReader module您应该使用 python 的csv.DictReader模块

import csv

data_dict = {}
with open(argv[1]) as data_file:
    reader = csv.DictReader(data_file)
    for record in reader:
        # `record` is a OrderedDict (type of dict) of column-name & value.
        # Instead of creating the data pair as below:
        # ```
        # name = record["name"]
        # data = {
        #     "AGATC": record["AGATC"],
        #     "AATG": record["AATG"],
        #     "TATC": record["TATC"],
        #     ...
        # }
        # data_dict[name] = data
        # ```
        # you can just delete the `name` column from `record`
        name = record["name"]
        del record["name"]
        data_dict[name] = record

print(data_dict)

Using simple file read使用简单的文件读取

with open(argv[1], 'r') as data_file:
  line = next(data_file)          # get the first line from file (i.e. header)
  hdr = line.rstrip().split(',')  # convert header string to comma delimited list
                                  # ['name', 'AGATC', 'AATG', 'TATC']
  
  data_dic = {}
  for line in data_file:
    line = line.rstrip().split(',')
    # name and dictionary for current line
    data_dic[line[0]] = {k:v for k, v in zip(hdr[1:], line[1:])}

print(data_dic)

Output Output

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM