0

I am finding patterns within a large text file, then I remove the patterns, and print the text. But, I am trying to replace the old text within the file (with the patterns) with the new_text without the patterns.

Im using the regex package, and no matter what i try it doesnt work

My command .replace is not working.

import re

rgx_list = ['Read More',
            'Read',
            'And follow us on Twitter to keep up with the latest news and and acute and primary Care.',...]

txt_path = '/Users/sofia/Documents/src/fakenews1/data/news-data/war_sc_r.txt'

with open(txt_path) as new_txt_file:
    new_text = new_txt_file.read()

for rgx_match in rgx_list:
        new_text = re.sub(rgx_match, '', new_text)

new_text.replace(txt_path, new_text)

print(new_text)

Thank you !

sofbi
  • 61
  • 1
  • 6
  • 2
    `with open(txt_path) as new_txt_file:` automatically opens the file in read mode. You're not writing anything back to the file. There are plenty of tutorials and questions/answers on reading and writing files. – roganjosh Aug 19 '18 at 16:22
  • Ive read every forum on stack overflow. nothing is helping me – sofbi Aug 19 '18 at 16:48
  • `replace` doesn't edit the string; it returns a new string. Strings are immutable. It looks like you think it replaces the file path content. – Mark Tolonen Aug 19 '18 at 17:42

1 Answers1

0

I do not understand why you are using replace() function. Maybe read up on the documentation for str.replace(). If you want to write the new_text to your file, you should open the file again in write mode then write the new content to the file.

import re

rgx_list = ['Read More',
            'Read',
            'And follow us on Twitter to keep up with the latest news and and acute and primary Care.']

txt_path = 'newyorktimes_test.txt'

with open(txt_path) as new_txt_file:
    new_text = new_txt_file.read()

for rgx_match in rgx_list:
        new_text = re.sub(rgx_match, '', new_text)

with open(txt_path, 'w') as new_txt_file:
  new_txt_file.write(new_text)

If you are looking for a more in place editing approach, there are libraries to help you just a Google search away.

Xnkr
  • 564
  • 5
  • 16