使用for循环创建嵌套字典

时间:2018-10-24 18:46:45

标签: python python-3.x csv dictionary

我有一个csv文件,我试图创建一个嵌套的字典,如下所示:

contacts = {"Tom": {"name": "Tom Techie",
                    "phone": "123 123546",
                    "email": "tom@tom.fi",
                    "skype": "skypenick"},

            "Mike": {"name": "Mike Mechanic",
                     "phone": "000 123546",
                     "email": "mike@mike.fi",
                     "skype": "-Mike-M-"}}

这是我写的:

file = open("csv","r")
d = {}
for i in file:

    f = i.strip()
    x = f.split(";")

    if x[4] != "":
        d.update({x[0] : {"name":x[1],
                      "phone":x[2],
                      "email":x[3],
                      "skype":x[4]}})

    else:
        d.update ({x[0] : {"name": x[1],
                       "phone": x[2],
                       "email": x[3]}})

但是,如上文所述,它将字典作为普通词典打印,并以更新作为键。

编辑: CSV的第一行:

key;name;phone;email;skype
Tom;Tom Techie;123 123546;tom@tom.fi;skypenick
Mike;Mike Mechanic;000 123456;mike@mike.fi;-Mike-M-
Archie;Archie Architect;050 987654;archie@archie

4 个答案:

答案 0 :(得分:2)

您可以使用pd.read_csv()to_dict()

import pandas as pd

contacts = pd.read_csv('test.csv', sep=';').set_index('key').to_dict(orient='index')

收益:

{'Tom': {'name': 'Tom Techie', 'phone': '123 123546', 'email': 'tom@tom.fi', 'skype': 'skypenick'}, 'Mike': {'name': 'Mike Mechanic', 'phone': '000 123456', 'email': 'mike@mike.fi', 'skype': '-Mike-M-'}, 'Archie': {'name': 'Archie Architect', 'phone': '050 987654', 'email': 'archie@archie', 'skype': nan}}

答案 1 :(得分:1)

我喜欢pandas的答案,但是如果您不想使用第三方库,请使用内置的csv模块:

import csv
from pprint import pprint

D = {}
with open('csv',newline='') as f:
    r = csv.DictReader(f,delimiter=';')
    for line in r:
        name = line['key']
        del line['key']
        D[name] = dict(line)

pprint(D)

输出:

{'Archie': {'email': 'archie@archie',
            'name': 'Archie Architect',
            'phone': '050 987654',
            'skype': None},
 'Mike': {'email': 'mike@mike.fi',
          'name': 'Mike Mechanic',
          'phone': '000 123456',
          'skype': '-Mike-M-'},
 'Tom': {'email': 'tom@tom.fi',
         'name': 'Tom Techie',
         'phone': '123 123546',
         'skype': 'skypenick'}}

答案 2 :(得分:0)

您可以使用zip()实现目标:

file = """key;name;phone;email;skype
Tom;Tom Techie;123 123546;tom@tom.fi;skypenick
Mike;Mike Mechanic;000 123456;mike@mike.fi;-Mike-M-
Archie;Archie Architect;050 987654;archie@archie""".splitlines()

d = {}
h = None
for i in file: # works the same for your csv-file
    # first row == header, store in h
    if h is None:
        h = i.strip().split(";")[1:]
        continue # done for first row

    x = i.strip().split(";")

    # zip pairs the read in line with the header line to get tuples
    # wich are fed into the dict constructor that creates the inner dict
    d[x[0]] = dict(zip(h,x[1:]+[""])) # no default for skype

    # use this instead if you want the skype key always present with empty default
    # d[x[0]] = dict(zip(h,x[1:]+[""]))  

print(d)

zip()丢弃较长列表的元素-您无需为此进行任何检查。

输出:

{'Tom':   {'name': 'Tom Techie', 'phone': '123 123546', 
           'email': 'tom@tom.fi', 'skype': 'skypenick'}, 
 'Mike':   {'name': 'Mike Mechanic', 'phone': '000 123456', 
            'email': 'mike@mike.fi', 'skype': '-Mike-M-'}, 
 'Archie': {'name': 'Archie Architect', 'phone': '050 987654', 
            'email': 'archie@archie'}}

如果使用带注释的行,则Skype的数据将获得默认值''-仅适用于b / c skype是分隔线的最后一个元素

答案 3 :(得分:0)

您可以使用dict理解!假设数据类似this here...

with open("df.csv", "r") as file:
  d = {x.split(";")[0]:{
      "name": x.split(";")[2], 
      "phone": x.split(";")[3],
      "email": x.split(";")[1], 
      "skype": x.split(";")[4][:-1] # Slice off trailing newline
  } for x in file}
  d.pop("")

我们希望尽可能使用with打开文件,以便从Python的上下文管理中受益。有关对with语句的基本理解,请参见https://www.python.org/dev/peps/pep-0343/

由于键""仅在csv的开头出现一次,因此我们可以在最后将其弹出,并避免在每次迭代时进行比较。 dict理解可以完成您想要使用d.update实现的功能。

有关理解的更多信息: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions

编辑:重构以删除对.split的重复调用,如下所示:

def line_to_dict(x, d):
  x = x.split(";")
  d[x[0]] = {
      "name": x[2], 
      "phone": x[3],
      "email": x[1], 
      "skype": x[4][:-1] # Slice off trailing newline
  }

with open("df.csv", "r") as file:
  d = {}
  for x in file:
    line_to_dict(x, d)
  d.pop("")