简体   繁体   English

Python-xlrd和unicodecsv

[英]Python - xlrd and unicodecsv

I want to convert an Excel(xlsx) file to csv. 我想将Excel(xlsx)文件转换为csv。 I have coded this function to do so, but quotechart='"' is not working. 我已经对此功能进行了编码,但是quotechart ='“'无法正常工作。

import xlrd
import unicodecsv

def xls_to_csv (xls_filename, csv_filename):

    wo = xlrd.open_workbook(xls_filename)
    st = wo.sheet_by_index(0)

    fl = open(csv_filename,"wb")
    csv_out = unicodecsv.writer(fl, encoding='utf-8', quotechart='"')

    for row_number in range (st.nrows):
        csv_out.writerow(st.row_values(row_number))

    fl.close()
xls_to_csv('PT_BR.POSTP.20160508_vx27.xlsx','prueba.csv')

current output: 当前输出:

Category,Term,POS,Term,POS,Term,POS,,,,
A001,atendimento,sust,concessionário,sust,não,adv,bom,adj,,
...

desired output: 所需的输出:

"Category","Term","POS","Term","POS","Term","POS"
"A001","atendimento","sust","concessionário","sust","não","adv","bom","adj"
...

Try adding this in csv file writer object instead of quotechart='"' : 尝试将其添加到csv文件quotechart='"'对象而不是quotechart='"'

quoting = unicodecsv.QUOTE_ALL

not tried though. 虽然没有尝试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM