René's URL Explorer Experiment


Title: Set Up Multiple Replacement Rules (Video) – Real Python

Open Graph Title: Set Up Multiple Replacement Rules – Real Python

Description: There are a few more replacements that you need to make to the transcript to get it into a format acceptable for independent review. Shorten or remove the timestamps and replace the usernames with Agent and Client. Now that you are starting to have…

Open Graph Description: There are a few more replacements that you need to make to the transcript to get it into a format acceptable for independent review. Shorten or remove the timestamps and replace the usernames with Agent and Client. Now that you are starting to have…

Opengraph URL: https://realpython.com/lessons/replace-string-python-multiple-rules/

X: @realpython

direct link

Domain: realpython.com


Hey, it has json ld scripts:
  {
    "@context": "https://schema.org",
    "@type": "VideoObject",
    "name": "Set Up Multiple Replacement Rules",
    "description": "There are a few more replacements that you need to make to the transcript to get it into a format acceptable for independent review. Shorten or remove the timestamps and replace the usernames with Agent and Client. Now that you are starting to have…",
    "thumbnailUrl": ["https://files.realpython.com/media/How-to-Replace-Parts-of-a-String-in-Python_Watermarked.a70c83972fa6.jpg"],
    "uploadDate": "2023-08-22T14:00:00+00:00",
    "duration": "PT4M14S",
    
    "potentialAction": {
      "@type": "SeekToAction",
      "target": "https://realpython.com/lessons/replace-string-python-multiple-rules/#t={seek_to_second_number}",
      "startOffset-input": "required name=seek_to_second_number"
    }
  }
  

authorReal Python
twitter:cardsummary_large_image
twitter:imagehttps://files.realpython.com/media/How-to-Replace-Parts-of-a-String-in-Python_Watermarked.a70c83972fa6.jpg
og:imagehttps://files.realpython.com/media/How-to-Replace-Parts-of-a-String-in-Python_Watermarked.a70c83972fa6.jpg
twitter:creator@realpython
og:typevideo.episode

Links:

https://realpython.com/
Start Herehttps://realpython.com/start-here/
Learn Python https://realpython.com/lessons/replace-string-python-multiple-rules/
Python Tutorials →In-depth articles and video courseshttps://realpython.com/search?kind=article&kind=course&order=newest
Learning Paths →Guided study plans for accelerated learninghttps://realpython.com/learning-paths/
Quizzes & Exercises →Check your learning progresshttps://realpython.com/quizzes/
Browse Topics →Focus on a specific area or skill levelhttps://realpython.com/tutorials/all/
Community Chat →Learn with other Pythonistashttps://realpython.com/community/
Office Hours →Live Q&A calls with Python expertshttps://realpython.com/office-hours/
Podcast →Hear what’s new in the world of Pythonhttps://realpython.com/podcasts/rpp/
Books →Round out your knowledge and learn offlinehttps://realpython.com/products/books/
Reference →Concise definitions for common Python termshttps://realpython.com/ref/
Code Mentor →BetaPersonalized code assistance & learning toolshttps://realpython.com/mentor/
Unlock All Content →https://realpython.com/account/join/
More https://realpython.com/lessons/replace-string-python-multiple-rules/
Learner Storieshttps://realpython.com/learner-stories/
Python Newsletterhttps://realpython.com/newsletter/
Python Job Boardhttps://www.pythonjobshq.com
Meet the Teamhttps://realpython.com/team/
Become a Contributorhttps://realpython.com/jobs/
Searchhttps://realpython.com/search
https://realpython.com/search
Joinhttps://realpython.com/account/join/
Sign‑Inhttps://realpython.com/account/login/?next=%2Flessons%2Freplace-string-python-multiple-rules%2F
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=replace-string-python
Unlock This Lessonhttps://realpython.com/account/join/?utm_source=rp_lesson&utm_content=replace-string-python
https://realpython.com/courses/replace-string-python/#team
Replacing a String in Pythonhttps://realpython.com/courses/replace-string-python/
Philipp Acsanyhttps://realpython.com/courses/replace-string-python/#team
Recommended Tutorialhttps://realpython.com/replace-string-python/
Course Slides (.pdf)https://realpython.com/courses/replace-string-python/downloads/replace-string-python-slides/
Sample Code (.zip)https://realpython.com/courses/replace-string-python/downloads/replace-string-python-code/
Ask a Questionhttps://realpython.com/lessons/replace-string-python-multiple-rules/#discussion
https://realpython.com/feedback/survey/course/replace-string-python/liked/?from=lesson-title
https://realpython.com/feedback/survey/course/replace-string-python/disliked/?from=lesson-title
Transcripthttps://realpython.com/lessons/replace-string-python-multiple-rules/#transcript
Discussionhttps://realpython.com/lessons/replace-string-python-multiple-rules/#discussion
00:00https://realpython.com/lessons/replace-string-python-multiple-rules/#t=0.53
There are a few more replacements that you need to make to the transcript to gethttps://realpython.com/lessons/replace-string-python-multiple-rules/#t=0.53
it into a format acceptable for independent review.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=4.32
Shorten or remove the timestamps and replace the usernames with Agent andhttps://realpython.com/lessons/replace-string-python-multiple-rules/#t=7.75
Client. Now that you are starting to have more strings to replace,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=12.04
chaining on .replace() is going to get repetitive.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=15.92
00:18https://realpython.com/lessons/replace-string-python-multiple-rules/#t=18.86
So let’s explore a better way to handle the replacements.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=18.86
00:24https://realpython.com/lessons/replace-string-python-multiple-rules/#t=24.14
Start with a file namedhttps://realpython.com/lessons/replace-string-python-multiple-rules/#t=24.14
transcript_multiple_replace.py and add the transcript as a multiline string.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=27.01
Okay, now let’s think a moment.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=32.26
The way .replace() works is that you provide an argument pair.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=34.57
00:38https://realpython.com/lessons/replace-string-python-multiple-rules/#t=38.37
The first argument is the string that you want to replace,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=38.37
and the second argument is the replacement string.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=41.61
That sounds a lot like a tuple, doesn’t it?https://realpython.com/lessons/replace-string-python-multiple-rules/#t=45.14
00:48https://realpython.com/lessons/replace-string-python-multiple-rules/#t=48.13
So one idea could be to keep a list of tuples with two items in each tuple.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=48.13
The two items would correspond to the arguments that you need to pass into thehttps://realpython.com/lessons/replace-string-python-multiple-rules/#t=53.77
.replace() method, the string to replace, and the replacement string.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=57.71
01:02https://realpython.com/lessons/replace-string-python-multiple-rules/#t=62.41
So let’s try that out.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=62.41
01:04https://realpython.com/lessons/replace-string-python-multiple-rules/#t=64.97
We make a list named replacements and add in the tuples.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=64.97
The first one contains "BLASTED" and the huffing emoji.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=69.17
The second one contains "Blast" and the huffing emoji.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=72.69
01:16https://realpython.com/lessons/replace-string-python-multiple-rules/#t=76.33
The first "BLASTED" is in uppercase,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=76.33
and the "Blast" right now is with an uppercase letter at the start.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=78.37
Then you add the first part of the timestamp,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=82.26
so that’s "2022-08-24"https://realpython.com/lessons/replace-string-python-multiple-rules/#t=85.21
and then uppercase "T", and an empty string ("") to replace it.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=89.53
01:35https://realpython.com/lessons/replace-string-python-multiple-rules/#t=95.26
Then you need to add the "+00:00".https://realpython.com/lessons/replace-string-python-multiple-rules/#t=95.26
You also replace this with an empty string. The "[support_tom]" username,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=99.45
which you replace with "Agent ".https://realpython.com/lessons/replace-string-python-multiple-rules/#t=104.52
01:46https://realpython.com/lessons/replace-string-python-multiple-rules/#t=106.76
Don’t forget the space behind the Agent because that will align the columnshttps://realpython.com/lessons/replace-string-python-multiple-rules/#t=106.76
better. And the tuple "[johndoe]" that you replace with "Client".https://realpython.com/lessons/replace-string-python-multiple-rules/#t=110.35
01:55https://realpython.com/lessons/replace-string-python-multiple-rules/#t=115.78
Note that Client doesn’t have a space at the end, so this way,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=115.78
"Client" and "Agent " with a space have both the same amount of characters and willhttps://realpython.com/lessons/replace-string-python-multiple-rules/#t=119.41
align nicely in your clean transcript.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=123.95
02:07https://realpython.com/lessons/replace-string-python-multiple-rules/#t=127.96
"[support_tom]" and "[johndoe"] both need to be in square brackets.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=127.96
Then you have a square bracket in line 17 to close the replacements list.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=131.25
02:17https://realpython.com/lessons/replace-string-python-multiple-rules/#t=137.13
With the list of replacements in place,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=137.13
you can iterate over the list and call .replace() on the transcript string.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=139.68
So, for old, new in replacements:https://realpython.com/lessons/replace-string-python-multiple-rules/#t=144.2
02:29https://realpython.com/lessons/replace-string-python-multiple-rules/#t=149.06
transcript = transcript.replace(old, new)https://realpython.com/lessons/replace-string-python-multiple-rules/#t=149.06
and then you print(transcript) in the end.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=155.39
02:39https://realpython.com/lessons/replace-string-python-multiple-rules/#t=159.03
In this version of your transcript-cleaning script,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=159.03
you created a list of replacement tables,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=161.55
which gives you a quick way to add replacements.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=163.69
You then iterate over the list of replacement tuples.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=166.71
02:49https://realpython.com/lessons/replace-string-python-multiple-rules/#t=169.71
In each iteration, you call .replace() on the string, populating thehttps://realpython.com/lessons/replace-string-python-multiple-rules/#t=169.71
arguments with the old and new variables that have been unpacked from eachhttps://realpython.com/lessons/replace-string-python-multiple-rules/#t=174.65
replacement table. With this,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=178.69
you’ve made a big improvement in the overall readability of the transcript.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=180.97
03:05https://realpython.com/lessons/replace-string-python-multiple-rules/#t=185.59
It’s also easier to add replacements if you need to.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=185.59
So let’s run the script and see what happens.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=189.14
03:13https://realpython.com/lessons/replace-string-python-multiple-rules/#t=193.68
Once you’re in the terminal,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=193.68
you can run python and then the name of your Python file,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=194.59
which is transcript_multiple_replace.py.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=197.26
03:21https://realpython.com/lessons/replace-string-python-multiple-rules/#t=201.98
And that’s a pretty clean transcript.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=201.98
You replace the swear words with the huffing emoji, and you replace the usernameshttps://realpython.com/lessons/replace-string-python-multiple-rules/#t=204.94
with Agent and Client. Also, the timestamps are much more readable now.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=209.52
03:34https://realpython.com/lessons/replace-string-python-multiple-rules/#t=214.55
Well done.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=214.55
Your script works perfectly with the provided transcript that you’ve got,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=216.11
but you may ask, what if you get a different transcript later that day?https://realpython.com/lessons/replace-string-python-multiple-rules/#t=220.62
03:45https://realpython.com/lessons/replace-string-python-multiple-rules/#t=225.57
Maybe there is another agent or another client. Also,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=225.57
replacing the swear words won’t work if there’s another variation—for example,https://realpython.com/lessons/replace-string-python-multiple-rules/#t=229.99
using "ing" or with a different capitalization. And you are right.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=233.76
03:58https://realpython.com/lessons/replace-string-python-multiple-rules/#t=238.49
These are valid concerns. But you know what?https://realpython.com/lessons/replace-string-python-multiple-rules/#t=238.49
I got you covered, just as if I already knew what concerns you would have.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=241.91
What a coincidence.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=246.98
You’ll learn how to handle all of these concerns in the next lesson.https://realpython.com/lessons/replace-string-python-multiple-rules/#t=248.73
Become a Memberhttps://realpython.com/account/join/
https://realpython.com/lessons/replace-string-python-chain/
Overviewhttps://realpython.com/courses/replace-string-python/
https://realpython.com/lessons/replace-string-python-regex-visualizer/
Replace a String in Python (Overview) 01:48 https://realpython.com/videos/replace-string-python-overview/
Investigate the Transcript 02:02 https://realpython.com/videos/replace-string-python-transcript/
Replace a String With .replace() 03:06 https://realpython.com/videos/replace-string-python-method/
Chain the .replace() Method 02:33 https://realpython.com/lessons/replace-string-python-chain/
Set Up Multiple Replacement Rules 04:14 https://realpython.com/lessons/replace-string-python-multiple-rules/
Use a Regex Visualizer 01:59 https://realpython.com/lessons/replace-string-python-regex-visualizer/
Find John Doe 02:14 https://realpython.com/lessons/replace-string-python-john-doe/
Work With a Token 02:04 https://realpython.com/lessons/replace-string-python-token/
Be Case-Insensitive 02:08 https://realpython.com/lessons/replace-string-python-case-insensitive/
Look for a Character Set 01:55 https://realpython.com/lessons/replace-string-python-character-set/
Append a Quantifier 02:18 https://realpython.com/lessons/replace-string-python-quantifier/
Recap Your Regex Patterns 00:13 https://realpython.com/lessons/replace-string-python-regex-patterns/
Get to Know re.sub() 01:09 https://realpython.com/lessons/replace-string-python-resub-intro/
Use re.sub() 05:11 https://realpython.com/lessons/replace-string-python-resub/
Replace a String in Python (Summary) 02:08 https://realpython.com/lessons/replace-string-python-summary/
Privacy Policyhttps://realpython.com/privacy-policy/

Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover

Robots: max-image-preview:large


URLs of crawlers that visited me.