将空类型转换为XLS行中的字符串

时间:2017-02-06 03:42:48

标签: python json excel list types

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

Python的xlrd很快并且能够解析文件,所以我需要将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))

但是,我收到类型错误:TypeError: empty:u'' is not JSON serializable

有没有办法将空类型转换为空字符串,以便我可以毫无顾虑地使用json

1 个答案:

答案 0 :(得分:0)

我是这样做的:

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