Hi,

I have got the following requirement:
In a file that has many lines, i need to search for a particular line that has the word DTP. The line may be like this in the file:
src 172.23.1.23 dst 192.14.2.34 DTP 0 - 65000
If the line with the word "DTP" is found, then that line has to be removed from the file.
How to do this?
Also in some cases I need to replace that word with another word and then write that line back to the file.
I have looked at some of the file programs that use readlines, read etc, but haven't found anything that solves my issues.
any help would be very much valuable for me.

Regards,
Prashanth

Recommended Answers

All 4 Replies

Here is one way to accomplish this. I don't know which version of Python you are using. If you have a newer version you may want to use a with statement to simplify this experimental code a little ...

# read a file line by line and write out a new file
# that excludes any line with a given search string

# raw data for the test file
test_data = """\
src 172.23.1.23 dst 192.14.2.34 DTQ 0 - 65000
src 172.23.1.23 dst 192.14.2.34 DTM 0 - 65000
src 172.23.1.23 dst 192.14.2.34 DTA 0 - 65000
src 172.23.1.23 dst 192.14.2.34 DTP 0 - 65000
src 172.23.1.23 dst 192.14.2.34 DTF 0 - 65000"""

# write the test file
fname_test = "atest_raw.txt"
fout = open(fname_test, "w")
fout.write(test_data)
fout.close()

# create a filename for the changed file
fname_out = "atest_out.txt"

# c make the file handles for the in and out files
fh_in = open(fname_test, "r")
fh_out = open(fname_out, "w")

# loop through the test file line by line
# and write only the lines back out that do
# not contain the search string
search = "DTP"
for line in fh_in:
    if search not in line:
        fh_out.write(line)

# close the file handles
fh_in.close()
fh_out.close()

# now you can take an editor to look at file "atest_out.txt"
# and see if the line with the search string in it is missing

This rather simple approach has a caveat, if "DTP" appears in the line inside another word like "ADTPW" this would also remove that line.

Should your file contain this possibility, you have to split each line into a list of words, or use module re. Also, if you know that "DTP" is always surrounded by spaces, you can change the search word to include the spaces " DTP "

Something like:

f_in = open('input.txt', 'r')
f_out = open('output.txt', 'w')

for line in f_in:
    if line.find('DTP') == 0:
        f_out.write(line)

or

f_in = open('input.txt', 'r')
f_out = open('output.txt', 'w')

for line in f_in:
    line.replace('DTP', 'NEW_WORD')
    f_out.write(line)

Sorry for the lazyness, but I'm not at home.

A simple test of the following code posted above shows that it will only find 'DTP' if it is at the beginning of the line. It should read as indicated.

f_in = ["xyz", "abcDTPxyz", "DTPabc"]
for line in f_in:

    ## the if statement should read
    ## if line.find('DTP') > -1: as line.find() returns the position found
    if line.find('DTP') == 0:
        print "DTP found in", line

""" 
prints
DTP found in DTPabc
only
"""

The intention was to give negative condition on the lines with 'DTP', and not to give positive.

Anyway a litle more refreshed.

f_in = open('input.txt', 'r')
f_out = open('output.txt', 'w')

for line in f_in:
    if 'DTP' not in line:
        f_out.write(line)
    else:
        f_out.write(line.replace('DTP', 'NEW_WORD'))

f_in.close()
f_out.close()
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.