Home > Mobile >  Writing to a new .txt file in Python?
Writing to a new .txt file in Python?

Time:06-19

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!

CodePudding user response:

You append your search results to a list, open a new text file and pass it your list before writing to file, like this..

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:
            search_results.append(search_result.group(1))

with open('newfile.txt', mode='wt', encoding='utf-8') as myfile:
    myfile.write('\n'.join(search_results))

CodePudding user response:

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.

CodePudding user response:

I would add the results to a string with new line characters separating lines instead of just printing.

Then I would open a new text file in a similar fashion as you do above, but this time to write. (hint: there are other optional parameters for the open() function)

CodePudding user response:

To edit a fine in python you will have to open it first using:

with open("file.txt", "w") as f:

The important parameter is "w" which indicates the mode: writing.

Then to edit it you can do:

f.write("test")
  • Related