繁体   English   中英

Excel 到 Json 与 Python xlrd

[英]Excel to Json with Python xlrd

我正在努力使用 python xlrd 将一些 excel 数据转换为 json

假设我有两列: ID Note 1 blue 1 green 1 yellow 2 white 3 green 3 black

我需要通过基于 ID 对数据进行分组来在 json 中呈现结果:

{
    "ID": "1",
    "notes": [
        {
            "note": "blue",
            "note": "green",
            "note": "yellow"
        }
    ]
},
{
    "ID": "2",
    "notes": [
        {
            "note": "white",

        }
    ]
},
{
    "ID": "3",
    "notes": [
        {
            "note": "green",
            "note": "black"
        }
    ]
}
import xlrd
from collections import OrderedDict
import simplejson as json

wb = xlrd.open_workbook(r'C:................\Desktop\a.xlsx')
sh = wb.sheet_by_index(0)

data_list = []

for rownum in range(1, sh.nrows):
    data = OrderedDict()


    row_values = sh.row_values(rownum)

    data['ID'] = row_values[0]
    data['Notes'] = row_values[1]

    ................................


        data_list.append(data)


j = json.dumps(data_list, indent = 4)

with open(r'C:....................json', 'w') as f:
    f.write(j)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM