簡體   English   中英

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

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

我正在研究 cs50 的 pset6、DNA,我想讀取一個看起來像這樣的 csv 文件:

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

我想要創建的是一個嵌套字典,它看起來像這樣:

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

所以我想用這個:

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

(或其他變體)循環遍歷csv文件和append到字典中,添加所有值,以便我有一個以后可以訪問的數據庫。

您應該使用 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)

使用簡單的文件讀取

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

{'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