grabbing portions of a file to output files

Discussion in 'Python' started by s99999999s2003@yahoo.com, May 22, 2006.

  1. Guest

    hi.
    I have a file with this kind of structure:

    Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    ..........
    ......
    ......
    xxxxx
    Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    ....
    .....
    ....
    xxxxx
    Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    ......
    .....
    and so on....lines starting with 'H' are headers. I wish to get the
    parts of the file
    where line start with 'H' all the way till before the next 'H' and save
    to files of different names...how is the best way to do it ?
    thanks
     
    , May 22, 2006
    #1
    1. Advertising

  2. Kent Johnson Guest

    wrote:
    > hi.
    > I have a file with this kind of structure:
    >
    > Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    > .........
    > .....
    > .....
    > xxxxx
    > Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    > ...
    > ....
    > ...
    > xxxxx
    > Hxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    > .....
    > ....
    > and so on....lines starting with 'H' are headers. I wish to get the
    > parts of the file
    > where line start with 'H' all the way till before the next 'H' and save
    > to files of different names...how is the best way to do it ?
    > thanks


    Something like this?

    out = None
    for line in open(...):
    if line.startswith('H'):
    if out:
    out.close()
    out = open(..., 'w')
    if out:
    out.write(line)
    out.close()

    Kent
     
    Kent Johnson, May 22, 2006
    #2
    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. Kevin Buchan
    Replies:
    2
    Views:
    730
    Kevin Buchan
    Dec 1, 2003
  2. Christian H
    Replies:
    0
    Views:
    294
    Christian H
    Aug 8, 2003
  3. ani
    Replies:
    7
    Views:
    2,109
    Alphonse Giambrone
    Jan 5, 2004
  4. Jeremy Chapman

    Reading portions of the web.config file

    Jeremy Chapman, Jun 22, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    403
    John Timney \(ASP.NET MVP\)
    Jun 22, 2005
  5. Nadie
    Replies:
    1
    Views:
    325
    Bengt Richter
    Aug 16, 2005
Loading...

Share This Page