2014-03-27 75 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