将空types从XLS行转换为string

业余时间:我必须使用Python,因为Ruby的Roo gem速度非常慢,并且Node.js可用的库无法parsing这些特定的XLSX文件(可能会损坏代码)?

Python的xlrd速度很快,能够parsing这些文件,因此我需要将XLSX文件的内容作为JSON转储到另一个文件中。

文档的前几行包含大量的空单元格,通过xlrd ,看起来像这样:

[empty:u'', empty:u'', text:u'loan Depot Daily Leads', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u'', empty:u''] 

我希望遍历列表并逐行转储JSON文件,如下所示:

 import xlrd import json book = xlrd.open_workbook("loan Depot Daily Leads.xlsx") # print("The number of worksheets is {0}".format(book.nsheets)) # print("Worksheet name(s): {0}".format(book.sheet_names())) sh = book.sheet_by_index(0) # print("{0} {1} {2}".format(sh.name, sh.nrows, sh.ncols)) # print("Cell D30 is {0}".format(sh.cell_value(rowx=29, colx=3))) with open("dumped.json", "a+") as myfile: for rx in range(sh.nrows): row = sh.row(rx) print(row) print(json.dumps(row)) myfile.write(json.dumps(row)) 

但是,我得到一个types错误: TypeError: empty:u'' is not JSON serializable

有没有办法将空types作为空string,所以我可以使用json而不用担心?

以下是我如何做到的:

 import xlrd import json import datetime book = xlrd.open_workbook("loan Depot Daily Leads.xlsx") sh = book.sheet_by_index(0) rows = [] for rx in range(sh.nrows): row = sh.row(rx) items = [] for cx, cell in enumerate(row): if sh.cell_type(rx, cx) == xlrd.XL_CELL_DATE: # This turns xlrd.xldate's float representation into # JSON-parseable string py_date = xlrd.xldate.xldate_as_datetime(cell.value, book.datemode) items.append(str(py_date)) elif cell.value == None: # NoneType will error out if you try to stringify it; # appending empty string instead items.append('') else: items.append(cell.value) rows.append(items) with open("leads.txt", "a+") as leadsfile: leadsfile.write(json.dumps(rows))