2

I have a simple Python snippet here which copies Macros Excel sheet into csvs. The snippet blows up for some odd reason. I used to run this snippet with Python 2.7 and had no problem with it. I recently downloaded Python 3.6. How can I fix this issue?

import csv import xlrd workbook = xlrd.open_workbook('P:/LFC Lots and Sales-NEW.xlsm') for sheet in workbook.sheets(): with open('{}.csv'.format(sheet.name), 'wb') as f: writer = csv.writer(f) writer.writerows(sheet.row_values(row) for row in range(sheet.nrows)) print ("Sheets copied") 

Traceback:

writer.writerows(sheet.row_values(row) for row in range(sheet.nrows)) TypeError: a bytes-like object is required, not 'str' 

1 Answer 1

2

Got it! All I needed to do was change the wb to w.

import csv import xlrd workbook = xlrd.open_workbook('P:/LFC Lots and Sales-NEW.xlsm') for sheet in workbook.sheets(): with open('{}.csv'.format(sheet.name), 'w') as f: writer = csv.writer(f) writer.writerows(sheet.row_values(row) for row in range(sheet.nrows)) print ("Sheets copied") 
Sign up to request clarification or add additional context in comments.

1 Comment

With python 3, you'll also want newline='' in your open -- see the docs here and read footnote #1.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.