text file reformatting
Braden Faulkner
bradenf at hotmail.com
Sun Oct 31 17:37:13 EDT 2010
Sorry to clarify, I was having issues getting this to work. I'm relatively new to Python. Sorry for the miscommunication.
> Date: Sun, 31 Oct 2010 16:13:42 -0500
> From: python.list at tim.thechases.com
> To: bradenf at hotmail.com
> CC: python-list at python.org
> Subject: Re: text file reformatting
>
> 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20101031/96f19aea/attachment-0001.html>
More information about the Python-list
mailing list