0

I have a text document and I need to add two @ symbols before the keywords present in an array.

Sample text and Array:

str ="This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of the employeeage:32"

arr=['name','employee_id','blood_group','age']

Required Text:

str ="This is a sample text document which consists of all demographic information of employee here is the value you may need, @@name: George @@employee_id:14296 @@blood_group:b positive this is the blood group of the employee @@age:32"

5 Answers 5

1

Just use the replace function

str ="This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of the employeeage:32"
arr = ['name','employee_id','blood_group','age']

for w in arr:
    str = str.replace(w, f'@@{w}')
print(str)
Sign up to request clarification or add additional context in comments.

Comments

0

You can simply loop over arr and use the str.replace function:

for repl in arr:
    strng.replace(repl, '@@'+repl)
    
print(strng)

However, I urge you to change the variable name str because it is a reserved keyword.

Comments

0

You might use re module for that task following way

import re
txt = "This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of the employeeage:32"
arr=['name','employee_id','blood_group','age']
newtxt = re.sub('('+'|'.join(arr)+')',r'@@\1',txt)
print(newtxt)

Output:

This is a sample text document which consists of all demographic information of employee here is the value you may need,@@name: George @@employee_id:14296@@blood_group:b positive this is the blood group of the employee@@age:32

Explanation: here I used regular expression to catch words from your list and replace each with @@word. This is single pass, as opposed to X passes when using multiple str.replace (where X is length of arr), so should be more efficient for cases where arr is long.

Comments

0

As an alternative, you can convert the below in a loop for lengthier list. There seems to be space before @@ too.

str= str[:str.find(arr[0])] + ' @@' + str[str.find(arr[0]):]

str= str[:str.find(arr[1])] + ' @@' + str[str.find(arr[1]):]

str= str[:str.find(arr[2])] + ' @@' + str[str.find(arr[2]):]

str= str[:str.find(arr[3])] + ' @@' + str[str.find(arr[3]):]

Comments

0

You can replace the value and add space and double @@ before the replaced value and in the result replace double spaces with one space.

str ="This is a sample text document which consists of all demographic information of employee here is the value you may need,name: George employee_id:14296blood_group:b positive this is the blood group of the employeeage:32"
arr=['name','employee_id','blood_group','age']

for i in arr:
    str = str.replace(i, " @@{}".format(i))
print(str.replace("  ", " "))

Output

This is a sample text document which consists of all demographic information of employee here is the value you may need, @@name: George  @@employee_id:14296 @@blood_group:b positive this is the blood group of the employee @@age:32

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.