Converting multiple tab-delimited .txt files into multiple .xls files

牧云@^-^@ 提交于 2019-11-29 11:31:09

Your encoding needs to be set for the output spreadsheet, I believe. You'd need to know what encoding that file is using. The csv module does not directly support unicode, but it's [8-bit-clean][1] so it just works for most western languages.

Without knowing what the encoding of your text file is, you have two options. Option 1 is use your local encoding according to python:

   >>> import locale
   >>> lang_code, encoding = locale.getdefaultlocale()

^^ Be careful using getdefaultlocale(). The documentation states that encoding MAY BE None.

OR just fallback to UTF8 and cross your fingers :D.

   >>> encoding = 'UTF8'
   >>> workbook = xlwt.Workbook(encoding=encoding)

You're not escaping the file names. For example, in Python the string "consulta_cand_2010\newName.xls" has "\n" in the middle, which is an end-of-line character --- invalid for a file name!

On Windows you need to write the literal strings containing file names "C:\\Like\\This" or "C:/Like/This" or even r"C:\Like\This".

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!