grabbing portions of a file to output files

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

  1. 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
     
    s99999999s2003, May 22, 2006
    #1
    1. Advertisements

  2. s99999999s2003

    Kent Johnson Guest

    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. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.