text file reformatting

Tim Chase python.list at tim.thechases.com
Sun Oct 31 17:13:42 EDT 2010


On 10/31/10 14:52, Braden Faulkner wrote:
>>     import csv
>>     f = file('def.csv', 'rb')
>>     f.next() # discard the header row
>>     r = csv.reader(f, delimiter=';')
>>     fields = [
>>       (varname, slice(int(start), int(start)+int(size)), width)
>>       for varname, start, size, width
>>       in r
>>       ]
>>     f.close()
>>     out = file('out.txt', 'w')
>>     try:
>>       for row in file('data.txt'):
>>         for varname, slc, width in fields:
>>           out.write(row[slc].ljust(width))
>>         out.write('\n')
>>     finally:
>>       out.close()
>
> I also am having issues with this.

[top-posting fixed -- it's generally frowned upon in this 
newsgroup/mailing-list and adherence to the preferences will tend 
to get you a wider audience]

Are your issues with my code, or with the topic at hand?  If it's 
my code, note my comment about it being untested.  If it's the 
topic at hand, I recommend trying my code (or a variation 
there-of after you've tested it).

-tkc





More information about the Python-list mailing list