例外:意外的数据类型<type'float'=“”>

时间:2016-05-04 22:49:27

标签: python xlrd xlwt

这有点像上一篇文章的延续,我应该说我仍然是python和编程的新手。我正在使用xlrd和xlwt来读取使用arcye功能类中使用arcpy创建的excel表,然后使用pyexcel_xls和xlutils来运行我的大脑(后两个在我的机器上没有正确安装,当时我无能为力)。

ArcMap会自动生成一个对象ID字段,在最终的excel输出上传到.dtt文件之前我需要将其删除,其中数组已经指定且无法更改。因此,脚本必须打开并读取使用arcpy创建的excel文件,将所有数据复制到不包括列0的dict中,然后使用xlwt将dict写入新工作簿。在克服了许多障碍后,除了当xlwt在将dict写入新的xls文件时遇到意外的数据类型时,我得到了一切工作:

    Traceback (most recent call last):
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 323, in RunScript
    debugger.run(codeObject, __main__.__dict__, start_stepping=0)
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\debugger\__init__.py", line 60, in run
    _GetCurrentDebugger().run(cmd, globals,locals, start_stepping)
  File "C:\Python27\ArcGIS10.2\Lib\site-packages\pythonwin\pywin\debugger\debugger.py", line 654, in run
    exec cmd in globals, locals
  File "N:\Python\Completed scripts\Recol1_SVRSTA00_prep2.py", line 2, in <module>
    import arcpy, xlrd, xlwt
  File "C:\Python27\ArcGIS10.2\lib\site-packages\xlwt\Worksheet.py", line 1030, in write
    self.row(r).write(c, label, style)
  File "C:\Python27\ArcGIS10.2\lib\site-packages\xlwt\Row.py", line 257, in write
    self.__rich_text_helper(col, label, style, style_index)
  File "C:\Python27\ArcGIS10.2\lib\site-packages\xlwt\Row.py", line 283, in __rich_text_helper
    raise Exception ("Unexpected data type %r" % type(data))
Exception: Unexpected data type <type 'float'>

我想知道我是否可以使用try,除了克服这个但我不确定如何编写except语句。否则,是否有另一种方法来克服这最后一个错误?这是脚本减去所有arcpy东西(工作正常)和导入模块语句:

workbook = xlrd.open_workbook(outXL)
writeBook = r"S:\Corporate\Corporate Information\Routesmart\Recol_1\Upload\SVRSTA00.xls"

sheet = workbook.sheet_by_index(0)

cell_range_value = 0
for value in sheet.col(32):
    cell_range_value += 1
print cell_range_value

dct1 = {}
for i in range(0, cell_range_value,1):
    data = [sheet.cell_value(i, col) for col in range(sheet.ncols)[1:]]
    cell_value_class = sheet.cell(i,32).value
    cell_value_id = data
    dct1[cell_value_class] = cell_value_id

workbook = xlwt.Workbook('utf-8','writeBook')
worksheet = workbook.add_sheet('SVRSTA00')

row = 0
col = 0

try:
    for key in dct1.keys():
        for value in dct1.values():
            worksheet.write(row, col, value)
        row += 1

except (RuntimeError,TypeError, NameError, ValueError):
    pass

workbook.save(writeBook)

0 个答案:

没有答案