我这里有一个简单的Python代码段,可将Macros Excel工作表复制到csvs中。片段由于某种奇怪的原因而爆炸。我曾经用Python 2.7运行此代码段,但没有问题。我最近下载了Python 3.6。如何解决此问题?

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


追溯:

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

最佳答案

得到它了!我要做的就是将wb更改为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")

关于python - Python-需要一个类似字节的对象,而不是'str',我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42371020/

10-13 06:35