gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 11:52:55 24 4
gpt4 key购买 nike

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

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

最佳答案

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

关于Python 将结果保存到 .xls 文件给出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21718325/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com