Home > front end >  How to replace multiple words in a sentence using python
How to replace multiple words in a sentence using python

Time:10-21

present_text="REASON FOR VISIT Referred by. Elevated PSA. HISTORY OF PRESENT ILLNESS JAMES EVERING is an 81 year old male. CURRENT MEDICATION  AmLODIPine Besylate 5 MG Tablet 90 days, 0 refills PREVIOUS THERAPY: History of education and instructions"
a = ["HISTORY OF PRESENT ILLNESS","CURRENT MEDICATION","ENCOUNTER NOTES","PREVIOUS THERAPY"]
for i in a:
    if i in present_text:
         first_update = present_text.replace(i,i ":")
         print(first_update)

Required soln: REASON FOR VISIT Referred by. Elevated PSA. HISTORY OF PRESENT ILLNESS: JAMES EVERING is an 81 year old male. CURRENT MEDICATION:  AmLODIPine Besylate 5 MG Tablet 90 days, 0 refills PREVIOUS THERAPY: History of education and instructions

Is there any solution to form a sentence with "" : "" adding to sentence ""HISTORY OF PRESENT ILLNESS:"" and ""CURRENT MEDICATION:"" and for "PREVIOUS THERAPY:" not need to add "" : "" since it is already present in sentence

CodePudding user response:

You would need to use regex replace function to get changes for all the strings.

import re

present_text="REASON FOR VISIT Referred by. Elevated PSA. HISTORY OF PRESENT ILLNESS JAMES EVERING is an 81 year old male. CURRENT MEDICATION  AmLODIPine Besylate 5 MG Tablet 90 days, 0 refills PREVIOUS THERAPY: History of education and instructions"

a = ["REASON FOR VISIT","HISTORY OF PRESENT ILLNESS","CURRENT MEDICATION","ENCOUNTER NOTES","PREVIOUS THERAPY"]

for i in a:

   present_text= re.sub(i, "\n" i ":", present_text)
  • Related