converting pipe delimited file to fixed width

Discussion in 'Python' started by digz, Mar 19, 2009.

  1. digz

    digz Guest

    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/python
    def rightFill(fillString, toLength, fillChar):
    return fillString+''.join([fillChar for x in range(len
    (fillString),toLength)])

    fieldWidth=[ 14, 6, 18, 21, 21,4, 6 ];

    file = open("/home/chatdi/input.csv", "r");
    lines = file.readlines()
    file.close()

    out = open( "/home/chatdi/ouptut.csv", 'w')
    for line in lines:
    line = line[:-1]
    index = 0
    for token in line.split('|'):
    paddedToken = rightFill(token, fieldWidth[index], ' ' )
    out.write( paddedToken )
    index = index + 1
    out.write("\n")
     
    digz, Mar 19, 2009
    #1
    1. Advertising

  2. digz

    MRAB Guest

    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
    >
    > #!/usr/bin/python
    > def rightFill(fillString, toLength, fillChar):
    > return fillString+''.join([fillChar for x in range(len
    > (fillString),toLength)])
    >
    > fieldWidth=[ 14, 6, 18, 21, 21,4, 6 ];
    >
    > file = open("/home/chatdi/input.csv", "r");
    > lines = file.readlines()
    > file.close()
    >
    > out = open( "/home/chatdi/ouptut.csv", 'w')
    > for line in lines:
    > line = line[:-1]
    > index = 0
    > for token in line.split('|'):
    > paddedToken = rightFill(token, fieldWidth[index], ' ' )
    > out.write( paddedToken )
    > index = index + 1
    > out.write("\n")
    >

    Here's my version:

    #!/usr/bin/python

    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("".join(padded_fields) + "\n")

    out.close()
     
    MRAB, Mar 19, 2009
    #2
    1. Advertising

  3. digz

    odeits Guest

    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 below . Any help
    > appreciated
    >
    > #!/usr/bin/python
    > def rightFill(fillString, toLength, fillChar):
    >     return fillString+''.join([fillChar for x in range(len
    > (fillString),toLength)])
    >
    > fieldWidth=[ 14, 6, 18, 21, 21,4, 6  ];
    >
    > file = open("/home/chatdi/input.csv", "r");
    > lines = file.readlines()
    > file.close()
    >
    > out = open( "/home/chatdi/ouptut.csv", 'w')
    > for line in lines:
    >     line = line[:-1]
    >     index = 0
    >     for token in line.split('|'):
    >         paddedToken = rightFill(token, fieldWidth[index], ' ' )
    >         out.write( paddedToken )
    >         index = index + 1
    >     out.write("\n")



    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.

    I use the csv module to do the reading of the file, i am going to do
    some testing to see if i can set the delimiter to '' so that i can
    also use it for the output. For now this should work.


    http://docs.python.org/library/csv.html


    import csv

    fieldWidth=[ 14, 6, 18, 21, 21,4, 6 ];
    csvfilepath = 'somefile.csv'

    outcsvfilepath = 'someoutfile.csv'
    endl = '\n'

    f = open(csvfilepath)
    outf = open(outcsvfilepath)


    csvfile = csv.reader(f,delimiter = '|')

    for row in csvfile:
    outrow = [ field + fillchar * (width - len(field)) for width, field
    in zip(fieldWidth,row)]
    outcsv.write( ''.join(outrow))
    outcsv.write( endl )

    f.close()
    outf.close()
     
    odeits, Mar 19, 2009
    #3
  4. digz

    Terry Reedy Guest

    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
    >
    > #!/usr/bin/python
    > def rightFill(fillString, toLength, fillChar):
    > return fillString+''.join([fillChar for x in range(len
    > (fillString),toLength)])


    Ugh. That should be the same as
    return fillString + (toLength-length(fillString))*fillChar
     
    Terry Reedy, Mar 19, 2009
    #4
  5. digz

    MRAB Guest

    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 help
    >> appreciated
    >>
    >> #!/usr/bin/python
    >> def rightFill(fillString, toLength, fillChar):
    >> return fillString+''.join([fillChar for x in range(len
    >> (fillString),toLength)])

    >
    > Ugh. That should be the same as
    > return fillString + (toLength-length(fillString))*fillChar
    >

    Ugh. That should be the same as
    return fillString.ljust(toLength, fillChar)

    (and there's no function length()) ;-)
     
    MRAB, Mar 19, 2009
    #5
  6. digz

    Tim Chase Guest

    > 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.csv")
    out = open("ouptut.csv", 'w')

    for fields in csv.reader(infile, delimiter='|'):
    padded_fields = [
    # pad, and then crop
    field.ljust(width)[:width]
    for field, width
    in zip(fields, field_widths)
    ]
    out.write("".join(padded_fields) + "\n")

    infile.close()
    out.close()

    If you want them right-justified, you can use

    field.rjust(width)[-width:]

    -tkc
     
    Tim Chase, Mar 19, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. johnp
    Replies:
    4
    Views:
    3,677
    Toby Inkster
    May 23, 2005
  2. mike beck
    Replies:
    2
    Views:
    712
    mike beck
    Sep 30, 2004
  3. RyanL
    Replies:
    6
    Views:
    691
    Paul McGuire
    Aug 28, 2007
  4. Elmo Watson

    DataReader to pipe delimited file question

    Elmo Watson, Jan 24, 2008, in forum: ASP .Net
    Replies:
    1
    Views:
    805
    Milosz Skalecki [MCAD]
    Jan 25, 2008
  5. Srikant
    Replies:
    1
    Views:
    500
    Greg Bacon
    Sep 29, 2007
Loading...

Share This Page