Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
352 views
in Technique[技术] by (71.8m points)

python - 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. It runs up to the point where I ask to save the xls output. 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:UsersAlineDocumentsData2013consulta_cand_2010
ewName.xls")
File "C:Python27libsite-packagesxlwtWorkbook.py", line 662, in save
doc.save(filename, self.get_biff_data())
File "C:Python27libsite-packagesxlwtWorkbook.py", line 637, in get_biff_data
shared_str_table   = self.__sst_rec()
File "C:Python27libsite-packagesxlwtWorkbook.py", line 599, in __sst_rec
return self.__sst.get_biff_record()
File "C:Python27libsite-packagesxlwtBIFFRecords.py", line 76, in get_biff_record
self._add_to_sst(s)
File "C:Python27libsite-packagesxlwtBIFFRecords.py", line 91, in _add_to_sst
u_str = upack2(s, self.encoding)
File "C:Python27libsite-packagesxlwtUnicodeUtils.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"
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You're not escaping the file names. For example, in Python the string "consulta_cand_2010 ewName.xls" has " " 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:LikeThis".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...