![](/img/trans.png)
[英]How to write a unique dictionary within dictionary with list as keys to a csv file?
[英]csv file to list within dictionary
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)
我的代码目前与示例输入类似:
我的 output 看起来像这样:
但是,我希望它看起来像这样:
{
"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)
它将在不使用pandas
或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, ...] })
如果源数据中的值是一个空单元格,则上面的代码将向output
添加一个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]))
for items in output.items():
print(items)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.