convert xls to json in python

ε祈祈猫儿з 提交于 2021-02-08 11:45:24

问题


I am trying to convert xls to json and but when I am executing the code it's not giving me the data inside xls sheet, it's only giving me the json structure. Below is the code which I am running, I am not able to understand what modification I should further make in this so that I can get a perfect json file.

Please note - input is in the form of binary stream and output is also in the form of a stream and not file.

    #!/usr/bin/python -u
import sys
import xlrd
import simplejson
from collections import OrderedDict

wb = xlrd.open_workbook(file_contents=sys.stdin.read())

for sheet_index in range(wb.nsheets):
#       print sheet_index
        sh =  wb.sheet_by_index(sheet_index)
       # print "Processing sheet no ", sheet_index
        attributes = sh.row_values(0)
        #print attributes
        rows_list = []
        attr_list = []
       # print attr_list[0]

        for rownum in range(1,sh.nrows):
                row_val_list = sh.row_values(rownum)
                row_dict = OrderedDict()
                for index in range(len(attr_list)):
                        row_dict[attr_list[index]] = row_val_list[index]

                #row_dict['ID'] = row_val_list[0]
                #row_dict['Name'] = row_val_list[1]

                #rows_list.append(row_dict)

        #json_data = simplejson.dumps(rows_list)
        #sys.stdout.write(json_data)
                rows_list.append(row_dict)
                json_data = simplejson.dumps(rows_list)
                sys.stdout.write(json_data)

#       json_data = simplejson.dumps(rows_list)

        #sys.stdout.write(json_data)
~

Any help is much appreciated


回答1:


here is the correct working python code

#!/usr/bin/python -u
import sys
import xlrd
import simplejson
from collections import OrderedDict

wb = xlrd.open_workbook(file_contents=sys.stdin.read())

#print "Sheets are .... ", wb.nsheets

for sheet_index in range(wb.nsheets):
        sh =  wb.sheet_by_index(sheet_index)

        if sh.nrows == 0:
                continue

        attr_list = sh.row_values(0)

        rows_list = []

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

                for index in range(len(attr_list)):
                        row_dict[attr_list[index]] = row_values[index]



                rows_list.append(row_dict)

        json_data = simplejson.dumps(rows_list)
        sys.stdout.write(json_data)


来源:https://stackoverflow.com/questions/43973140/convert-xls-to-json-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!