4

I am working on a project where I have to save the results to an excel file. 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!

1
  • Hi. Thanks! yes I understood what I was doing wrong. Commented Feb 12, 2014 at 4:25

2 Answers 2

1

The string you are writing is longer than is allowed by the interface - more than 32k (the maximum value of a signed short integer). You need to cut the strings into shorter pieces.

Sign up to request clarification or add additional context in comments.

1 Comment

If this happens during saving, how do I cut the string into pieces? Do you mean, split into different columns? How do I know which column/row gives this error?
1

The total number of characters that a cell can contain is 32,767 (reference here).

Use this to add characters of more than 32k

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

and save as .csv instead of .xls.

1 Comment

Welcome to SO. Please remember to format your code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.