2014-03-27 109 views
3

我使用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

任何人都知道可能會出錯?

回答

3

我建議使用pandas庫完成這個任務

import pandas as pd 
xls = pd.ExcelFile('file.xlsx') 
df = xls.parse(sheetname="Sheet1", index_col=None, na_values=['NA']) 
df.to_csv('file.csv') 
+0

甚至更​​短:'df = pd.read_excel(...)' – user2146414

1

你的問題基本上是你打開你的文件,Python2語義。 Python3是區域識別,所以如果你只想寫文本文件(和你),打開它用正確選項的文本文件:

your_csv_file = open('test_output.csv', 'w', encoding='utf-8', newline='')

編碼參數指定輸出編碼(它不一定是utf-8),csv的Python3文檔明確表示您應該爲csv文件對象指定newline=''

2

試試這個

import xlrd 
import csv 

def csv_from_excel(): 
    wb = xlrd.open_workbook('MySpreadsheet.xlsx') 
    sh = wb.sheet_by_name('Sheet1') 
    your_csv_file = open('output.csv', 'w', encoding='utf8') 
    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() 
0

更快的方法與pandas做到這一點:

import pandas as pd 

xls_file = pd.read_excel('MySpreadsheet.xls', sheetname="Sheet1") 
xls_file.to_csv('MySpreadsheet.csv', index = False) 
#remove the index because pandas automatically indexes the first column of CSV files. 

你可以閱讀更多關於pandas.read_excel here