'"Replace" from central file?

I am trying to extend the replace function. Instead of doing the replacements on individual lines or individual commands, I would like to use the replacements from a central text file.

That's the source:

import os
import feedparser
import pandas as pd

pd.set_option('max_colwidth', -1)

RSS_URL = "https://techcrunch.com/startups/feed/"

feed = feedparser.parse(RSS_URL)
entries = pd.DataFrame(feed.entries)

entries = entries[['title']]
entries = entries.to_string(index=False, header=False)
entries = entries.replace(' ', '\n')
entries = os.linesep.join([s for s in entries.splitlines() if s])

print(entries)

I want to be able to replace words from a RSS feed, from a central "Replacement"-file, witch So the source file should have two columns:Old word, New word. Like replace function replace('old','new').

Output/Print Example:

  • truck
  • rental
  • marketplace
  • D’Amelio
  • family
  • launches
  • to
  • invest
  • up
  • to
  • $25M
  • ...

In most cases I want to delete the words that are unnecessary for me, so e.g. replace('to',''). But I also want to be able to change special names, e.g. replace('D'Amelio','DAmelio'). The goal is to reduce the number of words and build up a kind of keyword radar.

Is this possible? I can't find any help Googling. But it could well be that I do not know the right terms or can not formulate.



Solution 1:[1]

with open('<filepath>','r') as r:
    # if you remove the ' marks from around your words, you can remove the [1:-1] part of the below code
    words_to_replace = [word.strip()[1:-1] for word in r.read().split(',')]

def replace_words(original_text, words_to_replace):
    for word in words_to_replace:
        original_text = original_text.replace(word, '')
    return original_text

Solution 2:[2]

I was unable to understand your question properly but as far as I understand you have strings like cat, dog, etc. and you have a file in which you have data with which you want to replace the string. If this was your requirement, I have given the solution below, so try running it if it satisfies your requirement. If that's not what you meant, please comment below.

TXT File(Don't use '' around the strings in Text File):

papa, papi
dog, dogo
cat, kitten

Python File: your_string = input("Type a string here: ") #string you want to replace

with open('textfile.txt',"r") as file1: #open your file
    lines = file1.readlines()

for line in lines: #taking the lines of file in one by one using loop
    string1 = f'{line}'
    string1 = string1.split() #split the line of the file into list like ['cat,', 'kitten']
    if your_string == string1[0][:-1]: #comparing the strings of your string with the file
        your_string = your_string.replace(your_string, string1[1]) #If string matches like user has given input cat, it will replace it with kitten.
        print(your_string)
    else:
        pass

If you got the correct answer please upvote my answer as it took my time to make and test the python file.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 5th
Solution 2 Tech X Help