Search code examples
pythonfiletext

Writing to a new .txt file in Python?


I'm working on a project that requires importing a text file, cleaning the data, and writing to a new text file. I need help with the last step. My Python program looks like this:

import re

with open("data.txt") as file:
    for line in file:
        search_result = re.search(r"^(An act (?:.+?)\.)", line)
        if search_result:
            print(search_result.group(1))

This successfully cleans up the text as needed and prints it. How would you modify it to write to a .txt file? Thank you!


Solution

  • there are a couple simple modification you can do, first of course is to know how to open a file to write, and that is simple passing the second optional argument "w"

    The first and simple option is to save the desire result into a list and when you're done, write those results into a file

    Example 1

    import re
    
    search_results = []
    with open("data.txt") as file:
        for line in file:
            search_result = re.search(r"^(An act (?:.+?)\.)", line)
            if search_result:
                result = search_result.group(1)
                print(result)
                search_results.append(result)
    
    with open("clean data.txt","w") as output_file:
        for r in search_results:
            output_file.write(r)
            output_file.write("\n") # don't forget to put the new line, write doesn't do it for you
    

    but what if we could print into a file? that way that way we wouldn't need to remember to put the new line, and the good thing is that we can, print can take a key-word only argument file that is, well, the file where we want the print's output goes into

    Example 2

    import re
    
    search_results = []
    with open("data.txt") as file:
        for line in file:
            search_result = re.search(r"^(An act (?:.+?)\.)", line)
            if search_result:
                result = search_result.group(1)
                print(result)
                search_results.append(result)
    
    with open("clean data.txt","w") as output_file:
        for r in search_results:
            print(r, file=output_file)
    

    but if we do that, why not do it along the previous print? and the answer is: yes we can, granted that we are done processing that piece of data, we can put it into the result file directly (otherwise do it like the previous example)

    Example 3

    import re
    
    with open("data.txt") as file, open("clean data.txt","w") as outfile:
        for line in file:
            search_result = re.search(r"^(An act (?:.+?)\.)", line)
            if search_result:
                result = search_result.group(1)
                print(result)
                print(result, file=outfile)
    

    and this is the final form, the with statement can take many a thing simultaneously and we use print extra potential.

    The next step would be to put that or part there off into a function, so it can be used for more that just those files more easily, but I leave that as an exercise for the reader.