Johq
Johq

Reputation: 11

How to replace part of text

I am really new to coding so don't be harsh on me, since my question is probably basic. I couldn't find a way to do it.

I would like to learn how to create automatizated process of creating custom links.(Preferably in Python) Let me give you example.

https://website.com/questions/ineedtoreplacethis.pdf

I have a database (text file) of names, one name one line (Oliver David Donald etc.)

I am looking for a way how to automatically insert the name to the "ineedtoreplacethis" part of the link and create many many custom links like that at once.

Thank you in advance for any help.

Upvotes: 1

Views: 59

Answers (5)

Gustav Rasmussen
Gustav Rasmussen

Reputation: 3961

from pprint import  pprint as pp
import re

url = "https://website.com/questions/ineedtoreplacethis.pdf"

pattern = re.compile(r"/(\w+)\.pdf")  # search the end of the link with a regex
sub_link = pattern.search(url).group(1)  # find the part to replace

print(f"{sub_link = }")

names = ["Oliver", "David", "Donald"]  # text file content loaded into list

new_urls = []
for name in names:
    new_url = url.replace(sub_link, str(name))
    new_urls.append(new_url)

pp(new_urls)  # Print out the formatted links to the console

Upvotes: 0

user5658788
user5658788

Reputation:

You can do this using string concatenation as explained below. This is after you get the data from the text file, achieving that is explained in the later part of the answer.

a= "Foo"

b= "bar"

a+b will return

"Foobar"

In your case,

original_link = "https://website.com/questions/"

sub_link = "ineedtoreplacethis.pdf"

out = original_link + sub_link

The value of out will be as you required.

To get the sub_link from your text file, read the text file as:

with open("database.txt","r") as file:
    data= file.readlines() # Here I am assuming that your text file is CRLF terminated

Once you have the data , which is a list of all the values from your text file, you can iterate using loops.

for sub_link in data:
    search_link = original_link+sub_link 

    """Use this search_link to do your further operations"

Upvotes: 1

CodeTalker
CodeTalker

Reputation: 1791

Consider file textFile.txt as

Oliver
David
Donald

You can simply loop over the names in the file as

with open("textFile.txt", "r") as f:
    name_list = f.read().split('\n')
link_prefix = 'https://website.com/questions/'
link_list = []

for word in name_list:
    link_list.append(link_prefix + word + '.pdf')

print(link_list)

This will print output as (ie. contents of link_list is):

['https://website.com/questions/Oliver.pdf', 'https://website.com/questions/David.pdf', 'https://website.com/questions/Donald.pdf']

Upvotes: 0

Emmanuel Di Pretoro
Emmanuel Di Pretoro

Reputation: 79

f-string is probably the way to go.

Here is an example:

names = ['Olivier', 'David', 'Donald']

for name in names:
  print(f"{name}.txt")

Output:

Olivier.txt
David.txt
Donald.txt

Upvotes: 3

Roshan
Roshan

Reputation: 724

Use a formatted string

filename = "test.txt"
lines = []
with open(filename) as my_file:
    lines = my_file.readlines()
for i in lines:
    print(f"https://website.com/questions/{i}.pdf")

EXPLAINATION:

  1. Read the txt file by a list of lines
  2. Iterate over the list using For loop
  3. Using formatted string print them

Upvotes: 0

Related Questions