【发布时间】:2020-09-26 02:43:29
【问题描述】:
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)
我的代码目前与示例输入类似:
我的输出如下所示:
但是,我希望它看起来像这样:
{
"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] }
}
谁能帮帮我
【问题讨论】:
-
字典项中的列表是否为
"orbital_period":[269.3, 874.774, 763.0]曾经有 3 个元素长? -
我在您想要的输出的第一行中理解了这本字典:
"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] }的数字来自哪里?在我看来,它们不在输入文件中...... -
它最初是一个 .csv 文件,但我只是在谷歌表格中格式化以便于访问。作业不希望我既不使用 panda 也不使用 csv 模块
-
您可以从电子表格中复制粘贴
标签: python python-3.x list csv dictionary