UnicodeDecodeError:'ascii'编解码器无法解码位置0的字节0xc2:序号不在范围内(128)

时间:2014-06-04 14:08:31

标签: python excel python-2.7 unicode xlwt

我编写了下面的代码,它提供了将使用XLWT模块创建的Excel文件的路径

master_path = r"C:\Users\nbt8ye8\Documents\Docs\Report Automation\KBE Reporting\Reports"
master_excel_file_raw = "KBE Master Data.xls"
master_excel_file = os.path.join(master_path, master_excel_file_raw)

然后在代码中我使用下面的代码创建Excel文件(没有问题):

master_excel_wbook = xlwt.Workbook()
master_excel_wsheet = master_excel_wbook.add_sheet("All Data", cell_overwrite_ok=True)
master_excel_wbook.save(master_excel_file)
master_excel_wbook.save(tempfile.TemporaryFile())

然而,当我运行代码时,它会给我以下错误。

Traceback (most recent call last):
File "C:\Users\nbt8ye8\workspace\Report Automation\import_data.py", line 1225, in <module>
createExcelFile()
File "C:\Users\nbt8ye8\workspace\Report Automation\import_data.py", line 1219, in createExcelFile
master_excel_wbook.save(master_excel_file)
File "build\bdist.win32\egg\xlwt\Workbook.py", line 662, in save
File "build\bdist.win32\egg\xlwt\Workbook.py", line 637, in get_biff_data
File "build\bdist.win32\egg\xlwt\Workbook.py", line 599, in __sst_rec
File "build\bdist.win32\egg\xlwt\BIFFRecords.py", line 76, in get_biff_record
File "build\bdist.win32\egg\xlwt\BIFFRecords.py", line 91, in _add_to_sst
File "build\bdist.win32\egg\xlwt\UnicodeUtils.py", line 50, in upack2
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 0: ordinal not in range(128)

有谁知道如何解决此问题?我已经尝试过对字符串进行编码和解码,到目前为止它还没有奏效,但我很可能没有正确地做到这一点。任何帮助都会非常感激。谢谢!

1 个答案:

答案 0 :(得分:0)

@RyanG是正确的,因为我的Excel文件中有unicode数据,所以抛出了这个错误。一旦我修改了Excel文件以删除unicode数据,问题就解决了。再次感谢。