0

When the below code is executed in Python 3.5 and xlwt 1.2.0, following error is generated: "Cannot convert byte objects to str implicitly" The code works fine for python 2.7. Anyone please let me know what could be the problem. Thanks in Advance!!

    import xlwt
    import re
    import os

    wb = xlwt.Workbook()
    ws = wb.add_sheet('A Test Sheet')
    ws_1 = wb.add_sheet('A Test Sheet_B')
    cnt_row = 0
    cnt_col_1 = 0
    cnt_col_2 = 0


    path = "E:\Python_Scripts"
    files = os.listdir("E:\Python_Scripts")
    for filename in files:
        if filename.endswith(".ptu"):
            fo = open(os.path.join(path, filename), 'r')

            while(1):
                str = fo.readline()
                if (str == ""):
                    print ("file finished")
                    break
                else:
                    matchObj = re.match(r'\s*  TEST (.*?).*', str)
                    if (matchObj):
                        str = str.split('TEST', 1)[1]
                        ws.write(cnt_row, 0, str)
                        matchObj_Author = re.match(r' (.*)  Author (.*?).*', str)
                    if (matchObj_Author):
                        str = str.split('(', 1)[1]
                        str = str.rsplit(')', 1)
                        ws.write(cnt_row, 1, str)
                        cnt_row = cnt_row + 1

            fo.close()
   wb.save('example.xls')
7
  • full stacktrace please... Commented Jan 12, 2017 at 15:12
  • I do not have full error info wright now, the problem is at last line, wb.save('example.xls') .... Commented Jan 12, 2017 at 15:20
  • are you sure your xlwt version is the one matching python 3? how did you perform the installation? Commented Jan 12, 2017 at 15:22
  • yeah,xlwt 1.2.0 is used on python 3.5.did it through downloaded setup.py file.As i was getting connction time out error through pip command. Commented Jan 12, 2017 at 15:27
  • 1
    btw don't use str as an identifier. Commented Jan 13, 2017 at 7:19

1 Answer 1

1

Your data input has changed. And one or more of its lines contain multiple strings.

If you're reading a file where a line has multiple entires then your str will be a list not a string. If it is a list, this will cause the error when invoking wb.save('example.xls'): TypeError: must be str, not bytes

Here's a pared down version of your program that I used to test this out:

import xlwt
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
ws_1 = wb.add_sheet('A Test Sheet_B')
cnt_row = 0
cnt_col_1 = 0
cnt_col_2 = 0

f = open('<an xml file with one string per line except the last line which has two strings', 'r', encoding='utf-8')
while 1:

    str = f.readline()
    wb.save('example.xls')
    if str == "":
        print ("file finished")
        break
    str = str.split('<', 1)[1]
    str = str.rsplit('<', 1)
    ws.write(cnt_row, 1, str)
    cnt_row = cnt_row + 1
    print('debug:last')
    print(str)
    print(type(str))

wb.save('example.xls')
f.close()
Sign up to request clarification or add additional context in comments.

3 Comments

I don't think that str is a list at any moment in the OP code
@Jean in the OP's code, what about str = str.rsplit(')', 1)?
Thank you all for your sugesstions. I removed 3.5 and installed 2.7,it is working.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.