Python将结果保存到.xls文件,从而产生错误

时间:2014-02-12 03:58:49

标签: python excel save

我正在开发一个项目,我必须将结果保存到excel文件中。我在项目结束时给出了命令,该命令一直正常,但现在给出了以下错误:

Traceback (most recent call last):
  File "C:\Users\5460\Desktop\Code\scikit-learn svm_body.py", line 139, in <module>
    main()
  File "C:\Users\5460\Desktop\Code\scikit-learn svm_body.py", line 136, in main
    wb.save('SVMResults_body001.xls')
  File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 662, in save
    doc.save(filename, self.get_biff_data())
  File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 637, in get_biff_data
    shared_str_table   = self.__sst_rec()
  File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 599, in __sst_rec
    return self.__sst.get_biff_record()
  File "C:\Python27\lib\site-packages\xlwt\BIFFRecords.py", line 76, in get_biff_record
    self._add_to_sst(s)
  File "C:\Python27\lib\site-packages\xlwt\BIFFRecords.py", line 91, in _add_to_sst
    u_str = upack2(s, self.encoding)
  File "C:\Python27\lib\site-packages\xlwt\UnicodeUtils.py", line 55, in upack2
    raise Exception('String longer than 32767 characters')
Exception: String longer than 32767 characters

我的部分代码如下:

def main():
    classifier = svm_learning(data_array, cat_array) #for training classifier
    classified = list(classifier.predict(test_array))
    print classified
    pred_array = np.array(classified)
    #write to file
    z = 1
    for c in classified:
        worksheet.write(z,1,c)#1 is for body
        z += 1
    scores = classifier.score(test_array, cat_test_array) #test_array,cat_test_array
    print scores
    crossval = cross_validation.cross_val_score(classifier,test_array,cat_test_array, cv=5) #5 folds -- cv=5
    print crossval
    print("Accuracy: %0.2f (+/- %0.2f)" % (crossval.mean(), crossval.std() * 2))
    print 'It is saving now'
    wb.save('SVMResults_body001.xls') 

关于我可能做错的任何想法?非常感谢!

2 个答案:

答案 0 :(得分:1)

您正在编写的字符串比接口允许的字符串长 - 超过32k(有符号短整数的最大值)。你需要将琴弦剪成更短的部分。

答案 1 :(得分:0)

一个单元格可以包含的字符总数为 32,767(参考 here)。

用它来添加超过 32k 的字符

Workbook wb = new Workbook(FileFormatType.CSV,CheckExcelRestriction='false')

并另存为 .csv 而不是 .xls