[英]Reading and writing multiple dictionaries to csv file vertically
I got multiple dictionaries in json format 'some.json' looking like these我有多个 json 格式“some.json”的字典,看起来像这些
{
"my_dict" : {
'key1': 'value1',
'key2': 'value2',
'key3': 'value3'
},
"my_dict2" : {
'key8': 'value8',
'key9': 'value9',
'key10': 'value10'
}
}
Both key and value are strings.键和值都是字符串。 I would like to export it to
csv
format vertically then read it back to json
file.我想将其垂直导出为
csv
格式,然后将其读回json
文件。 For example, when I add an item to my_dict on csv
format then it would also be added on the json
file.例如,当我以
csv
格式将项目添加到 my_dict 时,它也会添加到json
文件中。
Required output需要 output
my_dict
key1,value1
key2,value2
key3,value3
my_dict2
key8,value8
key9,value9
key10,value10
I got so far a solution, but the problem is, the name of the dictionaries are not written on the csv file so can't be read back to json file到目前为止我得到了一个解决方案,但问题是,字典的名称没有写在 csv 文件中,因此无法读回 json 文件
import pandas as pd
with open('some.json') as f_input:
df = pd.read_json(f_input)
df = df.bfill(axis='columns')
df.iloc[:, 0].to_csv('some.csv', encoding='utf-8', header=False)
I fixed your some.json
file:我修复了你的
some.json
文件:
{
"my_dict": {
"key1": "value1",
"key2": "value2",
"key3": "value3"
},
"my_dict2": {
"key8": "value8",
"key9": "value9",
"key10": "value10"
}
}
Now you can convert your json file into a csv file:现在您可以将 json 文件转换为 csv 文件:
pd.read_json('some.json', orient='index').stack().to_csv('some.csv', header=False)
Your some.csv
file looks like:您的
some.csv
文件如下所示:
my_dict,key1,value1
my_dict,key2,value2
my_dict,key3,value3
my_dict2,key8,value8
my_dict2,key9,value9
my_dict2,key10,value10
Note : with the above format, you can easily use your csv file in Excel if you need it.注意:使用上述格式,您可以在需要时轻松使用 Excel 中的 csv 文件。
The rollback operation:回滚操作:
d = pd.read_csv('some.csv', header=None, index_col=0).groupby(0, sort=False) \
.apply(lambda x: {k: v for k, v in zip(x[1], x[2])}).to_dict()
print(d)
# Output:
{'my_dict': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'},
'my_dict2': {'key8': 'value8', 'key9': 'value9', 'key10': 'value10'}}
Create new file with file.write
for names of dict:使用
file.write
为 dict 的名称创建新文件:
import json
with open('some.json') as f:
d = json.load(f)
#sample
d = { "my_dict" : { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3'},
"my_dict2" : { 'key8': 'value8', 'key9': 'value9', 'key10': 'value10'}}
with open("some1.csv", 'w') as f:
for k, v in d.items():
f.write(k + '\n')
for k1, v1 in v.items():
f.write(f"{k1},{v1}\n")
my_dict
key1,value1
key2,value2
key3,value3
my_dict2
key8,value8
key9,value9
key10,value10
And for read back:并回读:
df = pd.read_csv("some1.csv", names=['a','b'])
m = df['b'].isna()
df['new'] = df['a'].where(m).ffill()
s = df[~m].set_index(['new','a'])['b']
d = {level: s.xs(level).to_dict() for level in s.index.levels[0]}
print (d)
{'my_dict': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'},
'my_dict2': {'key8': 'value8', 'key9': 'value9', 'key10': 'value10'}}
EDIT:编辑:
If format should be changed:如果格式应该改变:
d = { "my_dict" : { 'key1': 'value1', 'key2': 'value2', 'key3': 'value3'},
"my_dict2" : { 'key8': 'value8', 'key9': 'value9', 'key10': 'value10'}}
with open("some1.csv", 'w') as f:
for k, v in d.items():
for k1, v1 in v.items():
f.write(f"{k},{k1},{v1}\n")
my_dict,key1,value1
my_dict,key2,value2
my_dict,key3,value3
my_dict2,key8,value8
my_dict2,key9,value9
my_dict2,key10,value10
s = pd.read_csv("some1.csv", names=['b'], squeeze=True)
print (s)
my_dict key1 value1
key2 value2
key3 value3
my_dict2 key8 value8
key9 value9
key10 value10
Name: b, dtype: object
d = {level: s.xs(level).to_dict() for level in s.index.levels[0]}
print (d)
{'my_dict': {'key1': 'value1', 'key2': 'value2', 'key3': 'value3'},
'my_dict2': {'key8': 'value8', 'key9': 'value9', 'key10': 'value10'}}
A little hacky but produces exactly the input you are after:有点hacky,但会产生您所追求的输入:
[f'{col}\n{df[col].dropna().to_csv(header=False)}' for col in df.columns)]
You can either ''.join(...)
and then write in a single go or add mode='a',
to to_csv
so that the output is appended directly to some file.您可以
''.join(...)
然后写入单个 go 或添加mode='a',
to_csv
以便 output 直接附加到某个文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.