我使用Python 3.3與xlrd和csv模塊將xls文件轉換爲csv。這是我的代碼:使用xlrd在Python 3中將xls轉換爲csv
import xlrd
import csv
def csv_from_excel():
wb = xlrd.open_workbook('MySpreadsheet.xls')
sh = wb.sheet_by_name('Sheet1')
your_csv_file = open('test_output.csv', 'wb')
wr = csv.writer(your_csv_file, quoting=csv.QUOTE_ALL)
for rownum in range(sh.nrows):
wr.writerow(sh.row_values(rownum))
your_csv_file.close()
有了,我收到此錯誤:TypeError: 'str' does not support the buffer interface
我試圖改變編碼,取而代之的是這個循環中的行:
wr.writerow(bytes(sh.row_values(rownum),'UTF-8'))
,但我得到此錯誤:TypeError: encoding or errors without a string argument
任何人都知道可能會出錯?
甚至更短:'df = pd.read_excel(...)' – user2146414