编写具有超过 2 个嵌套字典的 csv
这是我的示例字典:
nested_dict =
{
"value1_1": "X",
"value1_2": {
"value2_1": "Y",
"value2_2": {
"value3_1": "Z3_1",
"value3_2": "Z3_2"
...
}
...
}
我想将其转换为具有类似形式的 CSV:
X,Y,Z3_1,Z3_2
或者使用标题,但不希望有它:
Value1_1,Value2_1,Value3_1,Value3_2
X,Y,Z3_1,Z3_2
我找到了使用 DictWriter 的一些答案,但我不认为它支持超过 2 个嵌套字典。
我应该编写自己的 csv writer 吗?
Here is my sample dict:
nested_dict =
{
"value1_1": "X",
"value1_2": {
"value2_1": "Y",
"value2_2": {
"value3_1": "Z3_1",
"value3_2": "Z3_2"
...
}
...
}
I want to convert it to a CSV with a similar form:
X,Y,Z3_1,Z3_2
Or with the header but would prefer not having it:
Value1_1,Value2_1,Value3_1,Value3_2
X,Y,Z3_1,Z3_2
I've found some answers with using DictWriter but I couldn't figure it supporting more than 2 nested dict.
Should I write my own csv writer?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你不会找到一个作家这样做。也许做这样的事情?
它会将其写入
FILE.csv
:I do not think that you will find a writer doing that. Maybe do something like this?
It will write this to
FILE.csv
: