简体   繁体   English

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

[英]Python saving results to .xls file giving error

I am working on a project where I have to save the results to an excel file.我正在做一个项目,我必须将结果保存到一个 excel 文件中。 I am giving the command at the end of the project, which has been working fine, but now giving me the following error:我在项目结束时发出命令,该命令一直工作正常,但现在出现以下错误:

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

My part of the code is as below:我的部分代码如下:

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') 

Any ideas on what I might be doing wrong?关于我可能做错了什么的任何想法? Thanks a lot!非常感谢!

The string you are writing is longer than is allowed by the interface - more than 32k (the maximum value of a signed short integer).您正在写入的字符串比接口允许的长度更长 - 超过 32k(有符号短整数的最大值)。 You need to cut the strings into shorter pieces.您需要将琴弦切成较短的部分。

The total number of characters that a cell can contain is 32,767 (reference here ).一个单元格可以包含的字符总数为 32,767(参考此处)。

Use this to add characters of more than 32k使用它来添加超过 32k 的字符

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

and save as .csv instead of .xls .并另存为.csv而不是.xls

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM