Python:如何将Excel表格中的数据转换为json
这里是在excel中创建的表格,要转换成json数据。像这样:
{ "maintitle": {"idno": "10",
"subheading": [ { "prop": "val1",
"prop2": "val12",
"attrib": {"type": "well"},
"attri": {"pos": "[]"}},
{ "type": "ok",
"prop11": {"subprop1": "10",
"subprop2": "abc",
"subprop3": "0.202",
"subprop4": "moreval1"},
"moreprop": { "type": "dot",
"diffdata": ["3030.22","3903.22"]}
}
]
}
}
这里:以 [x] 结尾的列名称被读取为 python 列表,其下方的元素出现在索引 x 处。 不带 [x] 的列名被读取为 python 字典。
下面是代码,但它没有给出所需的结果:
from mergedeep import merge, Strategy
# data from excel is read & saved in variable columnlist
columnlist=[['maintitle', 'idno'],
['maintitle', 'subheading[0]', 'prop'],
['maintitle', 'subheading[0]', 'prop2'],
['maintitle', 'subheading[0]', 'attrib', 'type'],
['maintitle', 'subheading[0]', 'attri', 'pos'],
['maintitle', 'subheading[1]', 'type'],
['maintitle', 'subheading[1]', 'prop11', 'subprop1'],
['maintitle', 'subheading[1]', 'prop11', 'subprop2'],
['maintitle', 'subheading[1]', 'prop11', 'subprop3'],
['maintitle', 'subheading[1]', 'prop11', 'subprop4'],
['maintitle', 'subheading[1]', 'moreprop', 'type'],
['maintitle', 'subheading[1]', 'moreprop', 'diffdata[0]'],
['maintitle', 'subheading[1]', 'moreprop', 'diffdata[1]']]
jsondict={}
for col in columnlist:
tmpdict=''
for key in reversed(col):
if '[' in key:
tmpdict = {key: [tmpdict]}
else:
tmpdict = {key: tmpdict}
merge(jsondict,tmpdict, strategy=Strategy.ADDITIVE)
print(jsondict)
我认为这可以通过递归来实现。使用 python 如何完成?
Here is the table created in excel and is to be converted into json data. Like this :
{ "maintitle": {"idno": "10",
"subheading": [ { "prop": "val1",
"prop2": "val12",
"attrib": {"type": "well"},
"attri": {"pos": "[]"}},
{ "type": "ok",
"prop11": {"subprop1": "10",
"subprop2": "abc",
"subprop3": "0.202",
"subprop4": "moreval1"},
"moreprop": { "type": "dot",
"diffdata": ["3030.22","3903.22"]}
}
]
}
}
Here : Column names ending with [x] are read as python list having elements below it present at index x.
Column names without [x] are read as python dictionary.
Below is the code but it does not give the desired result:
from mergedeep import merge, Strategy
# data from excel is read & saved in variable columnlist
columnlist=[['maintitle', 'idno'],
['maintitle', 'subheading[0]', 'prop'],
['maintitle', 'subheading[0]', 'prop2'],
['maintitle', 'subheading[0]', 'attrib', 'type'],
['maintitle', 'subheading[0]', 'attri', 'pos'],
['maintitle', 'subheading[1]', 'type'],
['maintitle', 'subheading[1]', 'prop11', 'subprop1'],
['maintitle', 'subheading[1]', 'prop11', 'subprop2'],
['maintitle', 'subheading[1]', 'prop11', 'subprop3'],
['maintitle', 'subheading[1]', 'prop11', 'subprop4'],
['maintitle', 'subheading[1]', 'moreprop', 'type'],
['maintitle', 'subheading[1]', 'moreprop', 'diffdata[0]'],
['maintitle', 'subheading[1]', 'moreprop', 'diffdata[1]']]
jsondict={}
for col in columnlist:
tmpdict=''
for key in reversed(col):
if '[' in key:
tmpdict = {key: [tmpdict]}
else:
tmpdict = {key: tmpdict}
merge(jsondict,tmpdict, strategy=Strategy.ADDITIVE)
print(jsondict)
I think this can be achieved with recursion. How can it be done using python ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论