I have a test.txt like this:

GCOORD    5.98400000E+03  1.19901791E+01  8.29785919E+00 -1.10000002E+00
GCOORD    5.98500000E+03  1.25401793E+01  8.84785938E+00 -6.59999990E+00
GCOORD    5.98600000E+03  1.30901794E+01  9.39785957E+00 -1.21000004E+01
BNBCD     3.66000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     4.17000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          1.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.16000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          1.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.58000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.90000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     6.08000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00

That i want to do is remove the line and the line after this line (if line start with 'BNBCD' and this line contains '4.00000000E+00')
the result will be:

GCOORD    5.98400000E+03  1.19901791E+01  8.29785919E+00 -1.10000002E+00
GCOORD    5.98500000E+03  1.25401793E+01  8.84785938E+00 -6.59999990E+00
GCOORD    5.98600000E+03  1.30901794E+01  9.39785957E+00 -1.21000004E+01
BNBCD     4.17000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          1.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.16000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          1.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00

I am writing a script to run, but some errors, can someone help and tell how to make it run?
my code:

f = open("test.txt","r")
_file = open("file_08.txt", "w")
lines=f.readlines()
f.close()
count=-1
for line in lines:
    count=count+1
    if not line.strip():
            continue
    else:
            clean_line = line.strip().split()
            if clean_line[0]== 'BNBCD':
                    for i in range(len(clean_line)):
                        if clean_line[2] == '4.00000000E+00':
                            p=1
                            continue
            print p
            if p==1:
                #for i in range(len(clean_line)):
                if clean_line[0] == '4.00000000E+00':
                    p=0
                    continue
            else:
                _file.writelines(line)          
_file.close()

Recommended Answers

All 11 Replies

target = '4.0000000E+00'       
with open('test.txt') as infile, open('file_08.txt', 'w') as outfile:
    for line in infile:
        if not line.startswith('BNBCD ') or target not in line:
            outfile.write(line)

hi, thank you for the information, but with your code, when run it has Syntax error " There's a error in your program: invalid syntx".

Maybe something like this will work for you ...

data = '''\
GCOORD    5.98400000E+03  1.19901791E+01  8.29785919E+00 -1.10000002E+00
GCOORD    5.98500000E+03  1.25401793E+01  8.84785938E+00 -6.59999990E+00
GCOORD    5.98600000E+03  1.30901794E+01  9.39785957E+00 -1.21000004E+01
BNBCD     3.66000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     4.17000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          1.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.16000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          1.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.58000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.90000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     6.08000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
'''

fname = "test.txt"

# write the test file out
with open(fname, "w") as fout:
    fout.write(data)

# now ...
target = '4.00000000E+00'
with open('test.txt') as infile, open('file_08.txt', 'w') as outfile:
    for line in infile:
        tlist = line.split()
        if 'BNBCD' in tlist and target in tlist:
            continue
        elif target == tlist[0]:
            continue
        outfile.write(line)
        #print(line),  # test (Python27)

'''content of file 'file_08.txt' ...
GCOORD    5.98400000E+03  1.19901791E+01  8.29785919E+00 -1.10000002E+00
GCOORD    5.98500000E+03  1.25401793E+01  8.84785938E+00 -6.59999990E+00
GCOORD    5.98600000E+03  1.30901794E+01  9.39785957E+00 -1.21000004E+01
BNBCD     4.17000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          1.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.16000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          1.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
'''

You are probably not running latest Python 2, 2.7(3). You can use two nested with statements instead of two as at same line. I was writing from my mobile and had not possibility to test details. Also you must add logic to join lines which belong to same data block as I notice your data is not on single line. So check vegaseat's corrections to my code.

yes, sorry forgot to tell python version, my version is Python 2.6. I am wondering how to make it in this version.

hi, is there good method to run it in Python 2.6? I have tried with two nested with statements, but seems not work.
Thank you in advance!

Two nested with statements work in python 2.6; If your code fails, post the code and the traceback.

Hi all, thank you very much. yes modified with two nested with statements, it works!

hi vegaseat, my purpose is to remove the line and the line after this line (if line start with 'BNBCD' and this line contains '4.00000000E+00')
it seems your code will not work at some conditions.
for example:
if test.txt file is like this:

GCOORD    5.98400000E+03  1.19901791E+01  8.29785919E+00 -1.10000002E+00
GCOORD    5.98500000E+03  1.25401793E+01  8.84785938E+00 -6.59999990E+00
GCOORD    5.98600000E+03  1.30901794E+01  9.39785957E+00 -1.21000004E+01
BNBCD     3.66000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     4.17000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.16000000E+02  6.00000000E+00  4.00000000E+00  4.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.58000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00

what I want is:

GCOORD    5.98400000E+03  1.19901791E+01  8.29785919E+00 -1.10000002E+00
GCOORD    5.98500000E+03  1.25401793E+01  8.84785938E+00 -6.59999990E+00
GCOORD    5.98600000E+03  1.30901794E+01  9.39785957E+00 -1.21000004E+01
BNBCD     3.66000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00
BNBCD     5.58000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
          4.00000000E+00  0.00000000E+00  0.00000000E+00  0.00000000E+00

but after running your code it is:

GCOORD    5.98400000E+03  1.19901791E+01  8.29785919E+00 -1.10000002E+00
GCOORD    5.98500000E+03  1.25401793E+01  8.84785938E+00 -6.59999990E+00
GCOORD    5.98600000E+03  1.30901794E+01  9.39785957E+00 -1.21000004E+01
BNBCD     3.66000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00
BNBCD     5.58000000E+02  6.00000000E+00  1.00000000E+00  1.00000000E+00

in my code:

f = open("test.txt","r")
_file = open("file_08.txt", "w")
lines=f.readlines()
f.close()
count=-1
for line in lines:
    count=count+1
    if not line.strip():
        continue
    else:
        clean_line = line.strip().split()
        if clean_line[0]== 'BNBCD':
            for i in range(len(clean_line)):
                if clean_line[2] == '4.00000000E+00':
                    p=1
                    continue
       # print p
        if p==1:
            for i in range(len(clean_line)):
                if clean_line[0] == '4.00000000E+00':
                    p=0
                    continue
        else:
            _file.writelines(line)
_file.close()

I added a tempt variable 'p' to consider my purpose, unfortunately, Python 2.6 said,

Traceback (most recent call last):
  File "C:\Users\tony\Desktop\prescribed\Tony_own.py", line 18, in <module>
    if p==1:
NameError: name 'p' is not defined

any ideas?

Here:

target = '4.00000000E+00'
remove_next = False
with open('test.txt') as infile:
    with open('file_08.txt', 'w') as outfile:
        for line in infile:           
            if remove_next:            
                remove_next = False
                print 'removed next:\t', line,
            elif line.startswith('BNBCD') and target in line:
                remove_next = True
                print 'removed:\t', line,
            else:
                outfile.write(line)

thank you pytony, the code works! Thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.