Search code examples
pythonexcelxlrd

Python - xlrd and unicodecsv


I want to convert an Excel(xlsx) file to csv. I have coded this function to do so, but quotechart='"' is not working.

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"
...

Solution

  • Try adding this in csv file writer object instead of quotechart='"':

    quoting = unicodecsv.QUOTE_ALL
    

    not tried though.