阅读一个excel文件,并创build一个包括波兰语字符的最终文本文件

我需要能够阅读有很多波兰人字符的Excel文件。 然后我需要能够写这个文件保存波兰字符的文本文件。 到目前为止,我只能打开文件写一个,但每次它想写的Unicode值。 正如你可以从我的代码中看到的那样,在我写这个文件之前,我将u'去掉,但unicode的值是另一回事。

当我打开文本文件时,结束了这样的事情

[29178.0,Firma handlowa',Sklep farbiarsko-chemiczny',A-ZET ZHC CIEBIELSKI ZENO',LWOWEK',7880005802.0,CW PS',\ u1491czak Rafa \ u0142',ciebielski1@wp.pl',Nie',',17242.364799999999 ,1061.48,0.061562321196220141,Nie',0.0,1.0]

但是我希望它看起来像这样

29,178 Firma handlowa Sklep farbiarsko-chemiczny A-ZET ZHC CIEBIELSKI ZENO LWOWEK 7880005802 CW PSŁuczakRafałciebielski1@wp.pl Nie

wb = xlrd.open_workbook(xl_workbook.xls) #Get the sheet names sheets = wb.sheet_names() sheet1=[] for sheet in sheets: sheet1.append(sheet) #open the first sheet sh = wb.sheet_by_name(sheet1[0]) with open(xl_workbook.txt', "wb") as f: stri_line='' for rownum in xrange(sh.nrows): stri_line=str([val for val in sh.row_values(rownum)]) stri =str(stri_line.replace("u'","")) f.write(stri) 

一定有办法做到这一点。 很感谢任何forms的帮助。

用下面的代码replace你的代码的最后一部分:

 with open(xl_workbook+'.csv.txt', "wb") as f: sep=u"\t" #tab character for rownum in xrange(sh.nrows): stri_line=sep.join(unicode(val) for val in sh.row_values(rownum)) stri=stri_line.encode("utf8") # or any encoding you want that supports polish characters f.write(stri) f.write("\n") #newline 

string.replace()函数是故意删除unicode字符,让python知道如何编码这些字符。 定义文件句柄的编码,python会为你处理。

 with open('xl_workbook.txt', "w", encode="utf-8") as f: for rownum in xrange(sh.nrows): stri_line=str([val for val in sh.row_values(rownum)]) f.write(stri)