0

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!

fjturner
  • 23
  • 4
  • Welcome to Stack Overflow. Please read [ask] and https://meta.stackoverflow.com/questions/261592. What exactly is the problem you are running into, and what is your **specific** question about that problem? Do you know how to open a text file for writing? Do you know which file should be written to? Do you know how to write anything to such a file? Do you know what should be written to the file? If you put those pieces together, how does that not complete the task? Please try to think about the task logically, break it down into steps, and figure out where you are actually stuck. – Karl Knechtel Jun 18 '22 at 21:04

4 Answers4

1

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.

Copperfield
  • 8,131
  • 3
  • 23
  • 29
0

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))
Beatdown
  • 187
  • 2
  • 7
  • 20
-1

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)

Del
  • 11
  • 3
-1

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")