简体   繁体   中英

Excel to Json with Python xlrd

I am struggling to convert some excel data to json with python xlrd

Lets say I have two columns: ID Note 1 blue 1 green 1 yellow 2 white 3 green 3 black

I need to present the results in the json by grouping the data based on ID:

{
    "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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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