简体   繁体   English

将多个制表符分隔的.txt文件转换为多个.xls文件

[英]Converting multiple tab-delimited .txt files into multiple .xls files

I am a newbie to python and I am trying to do what the title above says with the code displayed below. 我是python的新手,我正在尝试使用下面显示的代码执行上面标题所说的内容。 It runs up to the point where I ask to save the xls output. 它运行到我要求保存xls输出的点。 Any help would be very much appreciated. 任何帮助将非常感谢。

import glob
import csv
import xlwt

for filename in glob.glob("C:\xxxx\*.txt"):
    wb = xlwt.Workbook()
    sheet = wb.add_sheet('sheet 1')
    newName = filename
    spamReader = csv.reader(open(filename, 'rb'), delimiter=';',quotechar='"')
    for rowx, row in enumerate(spamReader):
        for colx, value in enumerate(row):
            sheet.write(rowx, colx, value)

    wb.save(newName + ".xls")

print "Done"

Traceback (most recent call last):
File "C:/Users/Aline/Desktop/Python_tests/1st_trial.py", line 13, in <module>
wb.save("C:\Users\Aline\Documents\Data2013\consulta_cand_2010\newName.xls")
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 662, in save
doc.save(filename, self.get_biff_data())
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 637, in get_biff_data
shared_str_table   = self.__sst_rec()
File "C:\Python27\lib\site-packages\xlwt\Workbook.py", line 599, in __sst_rec
return self.__sst.get_biff_record()
File "C:\Python27\lib\site-packages\xlwt\BIFFRecords.py", line 76, in get_biff_record
self._add_to_sst(s)
File "C:\Python27\lib\site-packages\xlwt\BIFFRecords.py", line 91, in _add_to_sst
u_str = upack2(s, self.encoding)
File "C:\Python27\lib\site-packages\xlwt\UnicodeUtils.py", line 50, in upack2
us = unicode(s, encoding)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc7 in position 4: ordinal not in    range(128)

[edit] This code works. [编辑]此代码有效。

import glob
import csv
import xlwt

for filename in glob.glob("C:\\Users\\Aline\\Documents\\Data2013\\consulta_cand_2010\\*.txt"):
    spamReader = csv.reader((open(filename, 'rb')), delimiter=';',quotechar='"')
    encoding = 'latin1'
    wb = xlwt.Workbook(encoding=encoding)
    sheet=xlwt.Workbook()
    sheet = wb.add_sheet('sheet 1')
    newName = filename
    for rowx, row in enumerate(spamReader):
        for colx, value in enumerate(row):
            sheet.write(rowx, colx, value)
    wb.save(newName + ".xls")

print "Done"

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! 例如,在Python中,字符串"consulta_cand_2010\\newName.xls"在中间有"\\n" ,这是一个行尾字符---对于文件名无效!

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" . 在Windows上,您需要编写包含文件名"C:\\\\Like\\\\This""C:/Like/This"或甚至r"C:\\Like\\This"的文字字符串。

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. csv模块不直接支持unicode,但它是[8-bit-clean][1]所以它适用于大多数西方语言。

Without knowing what the encoding of your text file is, you have two options. 在不知道文本文件的编码是什么的情况下,您有两种选择。 Option 1 is use your local encoding according to python: 选项1根据python使用您的本地编码:

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

^^ Be careful using getdefaultlocale(). ^^小心使用getdefaultlocale()。 The documentation states that encoding MAY BE None . 文档说明编码可能是无

OR just fallback to UTF8 and cross your fingers :D. 或者只是回到UTF8并交叉你的手指:D。

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

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

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