0

I wish to create a function that will search each line of a input file, search through each line of this file looking for a particular string sequence and if it finds it, delete the whole line from the input file and output this line into a newly created text file with a similar format.

The input files format is always like so:

Firstname:DOB
Firstname:DOB
Firstname:DOB
Firstname:DOB

etc...

I want it so this file is input, then search for the DOB (19111991) and if it finds this string in the line then delete it from the input file and finally dump it into a new .txt document .

I'm pretty clueless if I'm being honest but I guess this would be my logically attempt even though some of the code may be wrong:

def snipper(iFile)
    with open(iFile, "a") as iFile:
    lines = iFile.readlines()
    for line in lines:
        string = line.split(':')
        if string[1] == "19111991":
            iFile.strip_line()
            with open("newfile.txt", "w") as oFile:
                iFile.write(string[0] + ':' + '19 November' + '\n')

Any help would be great.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Eric1989
  • 619
  • 3
  • 9
  • 17
  • What exactly is your question? Have you tested that code? Does it work? If not, have you tried to [debug it](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/)? – jonrsharpe Sep 18 '14 at 10:15
  • It doesn't work unfortunately. I just want the function to search for '19111991' in the input file and if it finds it, delete the line from the inputfile and dump it into a new output file. – Eric1989 Sep 18 '14 at 10:24
  • What do you mean *"doesn't work"*? Errors (provide full traceback)? Unexpected outputs (provide inputs and expected and actual outputs)? Where in the code is the problem (provide a [minimal example](http://stackoverflow.com/help/mcve))? In future, please follow [this checklist](http://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist). – jonrsharpe Sep 18 '14 at 10:52

1 Answers1

1

Try this code instead:

def snipper(filename)
    with open(filename, "r") as f:
        lines = f.readlines()

    new_data = filter(lambda x: "19111991" in x, lines)
    remaining_old_data = filter(lambda x: "19111991" not in x, lines)

    with open("newfile.txt", "w") as oFile:
        for line in new_data:
            oFile.write(line.replace("19111991", "19th November 1991'"))

    with open(filename, "w") as iFile:
        for line in remaining_old_data:
            iFile.write(line)
Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
  • @Eric1989 Check out [this](http://stackoverflow.com/a/890188/1860929) answer. In short, you can think of lambdas as one line functions. – Anshul Goyal Sep 18 '14 at 10:41
  • Thanks so much for your help - but just one more question. Would it be possible for you edit your code so the output of the new file is 'FirstName:19th November 1991'? Not sure if this is something practical or not. – Eric1989 Sep 18 '14 at 10:45
  • Hey, could you please edit your code so it cuts out 1911991 and 2011991 please? From this, I can do more with the code. Many thanks :) – Eric1989 Sep 18 '14 at 16:13
  • @Eric1989 that's not how SO works.. I've already answered your original question, if you have a new one, ask it as a new question instead :) – Anshul Goyal Sep 18 '14 at 21:31