简体   繁体   中英

python extract data from excel spreadsheet to json form

I would like to get Python to extract some values from an excel spreadsheet and send to webpage to be processed by javascript. not sure how to go about this the best way. i thought of creating a dict object and return this to js in the json form.

basically the values extracted from the excel spreadsheet will be as follow

Design Lump Sum:
design_cells[0].value, design_cells[1].value
design_cells[0].value, design_cells[1].value
design_cells[0].value, design_cells[1].value
design_cells[0].value, design_cells[1].value
design_cells[0].value, design_cells[1].value
...
..
.
Capex Lump Sum:
Capex_cells[0].value, Capex_cells[1].value
Capex_cells[0].value, Capex_cells[1].value
Capex_cells[0].value, Capex_cells[1].value
Capex_cells[0].value, Capex_cells[1].value
Capex_cells[0].value, Capex_cells[1].value
...
..
.

any help to have python create a dict or json object from the above.

see my extraction code below which iterates over the row values,

import xlrd
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import json
    book = xlrd.open_workbook("Glebe TE - Master Invoicing.xlsm")
    first_sheet = book.sheet_by_index(1)

    for i in range(12,19):
        design_cells = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)

        if str(design_cells[0].value) and str(design_cells[1].value):
            print str(design_cells[0].value)
            print str(design_cells[1].value)


    for i in range(22,155):
        capex_cells = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)
        if str(capex_cells[0].value) and str(capex_cells[1].value):
            print str(capex_cells[0].value)
            print str(capex_cells[1].value)


    for i in range(157,175):
        Opex_cells = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)
        if str(Opex_cells[0].value) and str(Opex_cells[1].value):
            j = j + 1
            print str(Opex_cells[0].value)
            print str(Opex_cells[1].value)

well, if you're print -ing, why not just pack them into an array, where the array element is a row, and either you have a dict with a single or just array of arrays:

alist = []    
tlist = []

for i in range(12,19):
    design_cells = first_sheet.row_slice(rowx=i, start_colx=2, end_colx=4)
    if str(design_cells[0].value) and str(design_cells[1].value):
        #print str(design_cells[0].value)
        #print str(design_cells[1].value)
        alist.append([str(design_cells[0].value), str(design_cells[1].value)]

alist would be: [ [0value, 1value], [0value, 1value],... ]

EDIT: OP wants a list of dicts (whose values are also lists) apparently:

tlist.append({'Design Sum': alist})
alist = [] # clear alist for next group

Then all you need is: json_dumps(tlist) to turn that object into a json string...

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