How do I remove patterns in text file?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



How do I remove patterns in text file?



I would like to remove patterns within in a text file (no_war_sc_r.txt). The .txt file holds mutliple articles that have word patterns I would like to remove.



I imported the 're' package that I found helps with this.



This is the code:



But, when I ran the code it gave me this error:
code erroe


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.', …]

new_text = open('/Users/sofia/Documents/src/fakenews1/data/news-data/no_war_pc_r_sophia.txt')

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

print(new_text)




1 Answer
1



This is because open() only creates and returns a file object, not a string. You can read the from the file using its read() method. Calling new_text.read() without any arguments will return a string containing the entire text of the file.


open()


read()


new_text.read()



Also, it is better to a context manager to interact with files,


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

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



You can learn more about Python file IO at https://docs.python.org/3/tutorial/inputoutput.html#reading-and-writing-files.



P.S. no need to shout, we understand you are here for python help. That's what the tags are for.





Hi, so great it works. How do I replace the old file with the new file (patterns removed?)
– sofbi
Aug 19 at 16:55






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

make 2 or more post in bootsrap

Store custom data using WC_Cart add_to_cart() method in Woocommerce 3

Firebase Auth - with Email and Password - Check user already registered