Home > Software design >  Re-assembling split filenames with python
Re-assembling split filenames with python

Time:12-01

I tried figuring this out with Powershell; but couldn't quite get there. Could someone please help me out with doing this in standard Python?

I'm having trouble re-assembling certain filenames (and discarding the rest) from a text file. The filenames are split up (usually on three lines) and there is always a blank line after each filename. I only want to keep filenames that begin with OPEN or FOUR. An example is:

OPEN.492820.EXTR
A.STANDARD.38383
333

FOUR.383838.282.
STAND.848484.NOR
MAL.3939

CLOSE.3480384.ST
ANDARD.39393939.
838383

The output I'd like (in a text file) would be:

OPEN.492820.EXTRA.STANDARD.38383333
FOUR.383838.282.STAND.848484.NORMAL.3939

Thanks for any suggestions!

CodePudding user response:

build = []
for line in open('xxx.txt'):
    line = line.rstrip()
    if line:
        build.append(line)
    else:
        if build[0][:4] in ('OPEN','FOUR'):
            print( ''.join(build), file=outfile )
        build = []
  • Related