简体   繁体   English

将数据结构:CSV 转换为字典

[英]Transforming data structure: CSV to dictionary

I have a text file like this:我有一个这样的文本文件:

Tomm Sietsema,Rooster and Owl,2.5,El Sapo Cuban Social Club,3.5,St. Anselm,3.0,Shibumi,3.5,Mama Chang,2.5,Punjab Grill,3.0    

Jonathan Golder,Rooster and Owl,3.0,El Sapo Cuban Social Club,3.5,St. Anselm,1.5,Shibumi,5.0,Punjab Grill,3.0,Mama Chang,3.5     

Brette Anderson,Rooster and Owl,2.5,El Sapo Cuban Social Club,3.0,Shibumi,3.5,Punjab Grill,4.0     

Michael Baumer,El Sapo Cuban Social Club,3.5,St. Anselm,3.0,Punjab Grill,4.5,Shibumi,4.0,Mama Chang,2.5    

Corby Kumar,Rooster and Owl,3.0,El Sapo Cuban Social Club,4.0,St. Anselm,2.0,Shibumi,3.0,Punjab Grill,3.0,Mama Chang,2.0    

Pete Wellsworth,Rooster and Owl,3.0,El Sapo Cuban Social Club,4.0,Punjab Grill,3.0,Shibumi,5.0,Mama Chang,3.5    

Jay Samuel,El Sapo Cuban Social Club,4.5,Mama Chang,1.0,Shibumi,4.0

I want output like this:我想要这样的 output :

{    'Tomm Sietsema': {'Rooster and Owl': 2.5, 'El Sapo Cuban Social Club': 3.5,
'The Godfather': 3.0, 'Shibumi': 3.5, 'Mama Chang': 2.5, 'Punjab Grill':
3.0},
    'Jonathan Golder': {'Rooster and Owl': 3.0, 'El Sapo Cuban Social Club':
3.5, 'The Godfather': 1.5, 'Shibumi': 5.0, 'Punjab Grill': 3.0, 'Mama Chang':
3.5}}     

I am new to Python.我是 Python 的新手。 I tried using list and appending the lists.我尝试使用列表并附加列表。 I don't know how to proceed after this.我不知道在这之后如何进行。

lists=list()    
with open(file_name) as b:       
for line in b:         
    lists.append(line.split(":")[0])   
    lists.append(line.split(":")[1])

Quite a bit shorter, and probably faster, than other solutions.比其他解决方案更短,而且可能更快。 Doing the loop as a dict comprehension should save some time.将循环作为字典理解进行应该可以节省一些时间。

with open(file_name) as fhin:
    for line in fhin:
        line = line.strip()
        if not line:
            continue

        entry = line.split(',')
        d[entry[0]] = {entry[i]: float(entry[i+1]) for i in range(1, len(entry)-1, 2)}

You may split your line using , then read the name at first, then read the elements by pair, and put all of this in nested dicts您可以使用 拆分您的行,然后首先读取名称,然后逐对读取元素,并将所有这些放在嵌套的字典中

# import json
result = {}  # outer dict

for line in lines:
    content = line.split(",")   # ['Tomm Sietsema', 'Rooster and Owl', '2.5', 'El Sapo Cuban Social Club', '3.5', 'St. Anselm', '3.0', 'Shibumi', '3.5', 'Mama Chang', '2.5', 'Punjab Grill', '3.0']
    name = content[0]           # 'Tomm Sietsema'
    name_values = {}            # inner dict

    for i in range(1, len(content), 2):
        val_name = content[i]              # read a element name
        val_float = float(content[i + 1])  # read the float value next
        name_values[val_name] = val_float  # save to inner dict

    result[name] = name_values             # save to outer dict

print(json.dumps(result, indent=4))

For the begnning it gives一开始它给了

{
    "Tomm Sietsema": {
        "Rooster and Owl": 2.5,
        "El Sapo Cuban Social Club": 3.5,
        "St. Anselm": 3.0,
        "Shibumi": 3.5,
        "Mama Chang": 2.5,
        "Punjab Grill": 3.0
    },
    "Jonathan Golder": {
        " Rooster and Owl": 3.0,
        " El Sapo Cuban Social Club": 3.5,
        " St.Anselm": 1.5,
        " Shibumi": 5.0,
        " Punjab Grill": 3.0,
        " Mama Chang": 3.5
    }
}

You can do something like this.你可以做这样的事情。

my_dict = {} 

with open("my_file.txt", "r") f:
    for line in f:
        key, *values = line.split(",") # key is user name, values are the rest
        tmp = {} # nested dict by user 
        val = iter(values) # smart way to create an iterator from the list
        for k in val:
             tmp[k] = float(next(val)) # add the key to the right value in the nested dict
        my_dict[key] = tmp # add the nested dict to the user

That produce the results you want.这会产生你想要的结果。

{ "Tomm Sietsema": { "Rooster and Owl": 2.5, "El Sapo Cuban Social Club": 3.5, "St. Anselm": 3.0, "Shibumi": 3.5, "Mama Chang": 2.5, "Punjab Grill": 3.0 }, "Jonathan Golder": { " Rooster and Owl": 3.0, " El Sapo Cuban Social Club": 3.5, " St.Anselm": 1.5, " Shibumi": 5.0, " Punjab Grill": 3.0, " Mama Chang": 3.5 } }

So your syntax for each line is所以你每行的语法是

name, key1, value1, key2, value2, ...

And you want to get你想得到

{name: {key1: value1, key2: value2, ...}}

This will give aa corresponding dict:这将给出一个相应的字典:

result = {}
for line in s.split("\n"):
    elements = line.split(",")
    name = elements[0].strip()    
    result[name] = {}

    # now iterate over key, value pairs across the rest of the elements
    for key, value in zip(elements[1::2], elements[2::2]):
        result[name][key.strip()] = float(value)

{'Tomm Sietsema': 
    {'Rooster and Owl': '2.5', 'El Sapo Cuban Social Club': '3.5', 'St. Anselm': '3.0', 'Shibumi': '3.5', 'Mama Chang': '2.5', 'Punjab Grill': '3.0'},
'Jonathan Golder':
    {'Rooster and Owl': '3.0', 'El Sapo Cuban Social Club': '3.5', 'St. Anselm': '1.5', 'Shibumi': '5.0', 'Punjab Grill': '3.0', 'Mama Chang': '3.5'},
'Brette Anderson':
    {'Rooster and Owl': '2.5', 'El Sapo Cuban Social Club': '3.0', 'Shibumi': '3.5', 'Punjab Grill': '4.0'},
'Michael Baumer':
    {'El Sapo Cuban Social Club': '3.5', 'St. Anselm': '3.0', 'Punjab Grill': '4.5', 'Shibumi': '4.0', 'Mama Chang': '2.5'},
'Corby Kumar':
    {'Rooster and Owl': '3.0', 'El Sapo Cuban Social Club': '4.0', 'St. Anselm': '2.0', 'Shibumi': '3.0', 'Punjab Grill': '3.0', 'Mama Chang': '2.0'},
'Pete Wellsworth':
    {'Rooster and Owl': '3.0', 'El Sapo Cuban Social Club': '4.0', 'Punjab Grill': '3.0', 'Shibumi': '5.0', 'Mama Chang': '3.5'},
'Jay Samuel':
    {'El Sapo Cuban Social Club': '4.5', 'Mama Chang': '1.0', 'Shibumi': '4.0'}
}

With the CSV import it is quite simple使用 CSV 导入非常简单

import csv

dict = {}

with open('input.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    line_count = 0
    for row in csv_reader:
        try:
            dict[row[0]] = {}
            for i in range(0,len(row),2):
                dict[row[0]].update({row[i+1]:row[i+2]})
        except:
            pass
        line_count += 1

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

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