1
我有一個簡單的Python代碼片段,它將宏Excel工作表複製到csvs中。這段代碼出於某種奇怪的原因而爆炸。我曾經運行這個片段Python 2.7
,並沒有問題。我最近下載了Python 3.6
。我該如何解決這個問題?Python - 需要類似字節的對象,而不是'str'
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'
使用python 3,你還需要在你的open中使用'newline =''' - 參見文檔[here](https://docs.python.org/3/library/csv.html #csv.writer)並閱讀腳註#1。 – DSM