Python将结果保存到.xls文件中给出错误

我正在一个项目,我必须将结果保存到一个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') 

任何想法,我可能做错了什么? 非常感谢!

您正在写入的string比接口允许的长度多 – 超过32k(带符号短整数的最大值)。 你需要把琴弦切成短片。