要在字典中列出的csv文件 [英] csv file to list within dictionary

查看:113
本文介绍了要在字典中列出的csv文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

with open('exoplanets.csv') as infile:
    planets = {} 
    lines = infile.readline()
    for line in infile:
        reader = csv.reader(infile)
        number =  [line]

        methods, number, orbital_period, mass, distance, year = (s.strip(' ') for s in line.split(','))
        planets[methods] = (number, orbital_period, mass, distance, year)
        print(planets)

我的代码当前与示例输入类似:

my code currently looks like that with the sample input:

我的输出看起来像这样:

and my output looks like this:

但是,我希望它看起来像这样:

however, I want it to look like this:

{
  "Radial Velocity" : {"number":[1,1,1], "orbital_period":[269.3, 874.774, 763.0], "mass":[7.1, 2.21, 2.6], "distance":[77.4, 56.95, 19.84], "year":[2006.0, 2008.0, 2011.0] } , 
  "Transit" : {"number":[1,1,1], "orbital_period":[1.5089557, 1.7429935, 4.2568], "mass":[], "distance":[200.0, 680.0], "year":[2008.0, 2008.0, 2008.0] }
}

有人可以帮我吗

推荐答案

检查此代码:

# import nan
from math import nan

# define source file
filename = 'EXOPLANETS.CSV - Sheet1.csv'

# read source file
with open(filename, 'r') as file:
    data = file.readlines()

# prepare output dictionary
output = {}

# read line by line
for idx, line in enumerate(data, 0):

    # split columns
    items = line.replace('\n', '').split(',')

    # extract inner dictionary's keys in a list: 'number', orbital_period', 'mass', 'distance', 'year'
    if idx == 0:
        values = [key for key in items[1:]]

    else:

        # add main key to the output dictionary: 'Radial Velocity', 'Imaging', 'Transit'
        if items[0] not in output.keys():
            output[items[0]] = {key : [] for key in values}

        # add value to the inner dictionary
        for jdx, key in enumerate(values, 1):

            # if the value is a valid number, convert it in float
            if items[jdx] != '':
                output[items[0]][key].append(float(items[jdx]))

            # if the value is not a valid number (empty cell), add a 'nan'
            else:
                output[items[0]][key].append(nan)

for items in output.items():
    print(items)

它将在不使用pandascsv的情况下执行您的任务:

It will perform your task without using neither pandas or csv:

("Radial Velocity" : {"number":[1.0, 1.0, ...], "orbital_period":[269.3, 874.774, ...], "mass":[7.1, 2.21, ...], "distance":[77.4, 56.95, ...], "year":[2006.0, 2008.0, ...] ),

("Imaging" : {"number":[1.0, 1.0, ...], "orbital_period":[nan, nan, ...], "mass":[nan, nan, ...], "distance":[45.52, 165.0, ...], "year":[2005.0, 2007.0, ...] ),

("Transit" : {"number":[1.0, 1.0, ...], "orbital_period":[1.5089557, 1.7429935, ...], "mass":[nan, nan, ...], "distance":[nan, 200.0, ...], "year":[2008.0, 2008.0, ...] })

如果源数据中的值是一个空单元格,则上面的代码会将nan添加到output.如果这是不想要的行为,并且您想跳过Empy单元格,请使用以下代码:

If the value in the source data is an empty cell, the code above will add a nan to the output. If this is an unwanted behavior and you want to jump the empy cells, use this code below:

# define source file
filename = 'EXOPLANETS.CSV - Sheet1.csv'

# read source file
with open(filename, 'r') as file:
    data = file.readlines()

# prepare output dictionary
output = {}

# read line by line
for idx, line in enumerate(data, 0):

    # split columns
    items = line.replace('\n', '').split(',')

    # extract inner dictionary's keys in a list: 'number', orbital_period', 'mass', 'distance', 'year'
    if idx == 0:
        values = [key for key in items[1:]]

    else:

        # add main key to the output dictionary: 'Radial Velocity', 'Imaging', 'Transit'
        if items[0] not in output.keys():
            output[items[0]] = {key : [] for key in values}

        # add value to the inner dictionary
        for jdx, key in enumerate(values, 1):

            # if the value is a valid number, convert it in float
            if items[jdx] != '':
                output[items[0]][key].append(float(items[jdx]))

for items in output.items():
    print(items)

这篇关于要在字典中列出的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆