Python:如何将Excel表格中的数据转换为json

发布于 2025-01-11 06:47:32 字数 2232 浏览 0 评论 0原文

输入图片此处说明

这里是在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 如何完成?

enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文