Re: converting pipe delimited file to fixed width

2009-03-19 Thread MRAB
John Posner wrote: [snip] field_widths = [14, 6, 18, 21, 21, 4, 6] out = open("/home/chatdi/ouptut.csv", 'w') for line in open("/home/chatdi/input.csv", "r"): fields = line.rstrip().split('|') padded_fields = [field.ljust(width) for field, width in zip(fields, field_widths)] out.

Re: converting pipe delimited file to fixed width

2009-03-19 Thread John Posner
[snip] > field_widths = [14, 6, 18, 21, 21, 4, 6] > > out = open("/home/chatdi/ouptut.csv", 'w') > for line in open("/home/chatdi/input.csv", "r"): > fields = line.rstrip().split('|') > padded_fields = [field.ljust(width) for field, width in zip(fields, > field_widths)] > out.write

Re: converting pipe delimited file to fixed width

2009-03-19 Thread Tim Chase
Caveat: none of the solutions (including mine) deal with the case of the field being longer than the width. You might want to throw an exception. Alternatively, you can just crop the results. Tweaking MRAB's elegant solution: field_widths = [14, 6, 18, 21, 21, 4, 6] infile = open("input.

Re: converting pipe delimited file to fixed width

2009-03-19 Thread MRAB
Terry Reedy wrote: digz wrote: Hi, I am trying to convert a | delimited file to fixed width by right padding with spaces, Here is how I have written the program , just get the feeling this can be done in a much better ( python functional ) way rather than the procedural code i have below . Any

Re: converting pipe delimited file to fixed width

2009-03-19 Thread odeits
On Mar 19, 8:51 am, digz wrote: > Hi, > I am trying to convert a | delimited  file to fixed width by right > padding with spaces, Here is how I have written the program , just get > the feeling this can be done in a much better ( python functional ) > way rather than the procedural code i have bel

Re: converting pipe delimited file to fixed width

2009-03-19 Thread Terry Reedy
digz wrote: Hi, I am trying to convert a | delimited file to fixed width by right padding with spaces, Here is how I have written the program , just get the feeling this can be done in a much better ( python functional ) way rather than the procedural code i have below . Any help appreciated #!

Re: converting pipe delimited file to fixed width

2009-03-19 Thread MRAB
digz wrote: Hi, I am trying to convert a | delimited file to fixed width by right padding with spaces, Here is how I have written the program , just get the feeling this can be done in a much better ( python functional ) way rather than the procedural code i have below . Any help appreciated #!

converting pipe delimited file to fixed width

2009-03-19 Thread digz
Hi, I am trying to convert a | delimited file to fixed width by right padding with spaces, Here is how I have written the program , just get the feeling this can be done in a much better ( python functional ) way rather than the procedural code i have below . Any help appreciated #!/usr/bin/pytho