dictionary = {'number': [2.1607142857142856, 1.5160021597156612, 1.0, 6.0],
'orbital_period': [847.5717667678571, 1796.522108459915, 0.73654, 14002.0],
'mass': [2.89239793814433, 4.4152433928281205, 0.0036, 21.42],
'distance': [39.760093457943924, 50.97704197461588, 1.35, 354.0],
'year': [2007.4732142857142, 5.01701576965303, 1995.0, 2013.0]}
zd = zip(*dictionary.values())
with open('file.csv', 'w') as file:
writer = csv.writer(file, delimiter=',')
writer.writerow(dictionary.keys())
writer.writerows(zd)
How do I read this file back to see if I get the desired output? Will this even give me the output
I want the output to have the attributes in the first column and the list of numbers as the row:
This might be a good time to look at pandas.
import pandas as pd
dictionary = {'number': [2.1607142857142856, 1.5160021597156612, 1.0, 6.0],
'orbital_period': [847.5717667678571, 1796.522108459915, 0.73654, 14002.0],
'mass': [2.89239793814433, 4.4152433928281205, 0.0036, 21.42],
'distance': [39.760093457943924, 50.97704197461588, 1.35, 354.0],
'year': [2007.4732142857142, 5.01701576965303, 1995.0, 2013.0]}
df = pd.DataFrame.from_dict(dictionary)
df.to_csv('output.csv', index=False)
To read it back in
df = pd.read_csv('output.csv')
The dataframe will look like this
number orbital_period mass distance year
0 2.160714 847.571767 2.892398 39.760093 2007.473214
1 1.516002 1796.522108 4.415243 50.977042 5.017016
2 1.000000 0.736540 0.003600 1.350000 1995.000000
3 6.000000 14002.000000 21.420000 354.000000 2013.000000
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.