Viewers: in countries Watching now:
Due to its power, simplicity, and complete object model, Python has become the scripting language of choice for many large organizations, including Google, Yahoo, and IBM. In Python 3 Essential Training, Bill Weinman demonstrates how to use Python 3 to create well-designed scripts and maintain existing projects. This course covers the basics of the language syntax and usage, as well as advanced features such as objects, generators, and exceptions. Example projects include a normalized database interface and a complete working CRUD application. Exercise files accompany the course.
Let's take a look at how we can do search and replace using regular expressions in Python. Start by making a working copy of regex.py. Call it regex-working.py. We'll open our working copy and here we have our little script that searches for a particular pattern in a file. If we run this, you see it finds all the places where either Lenore or Nevermore is found in Edgar Allan Poe's "The Raven." So let's do a search and replace instead of just a plain search.
Search and replace in Python is done with re.sub, so we'll change this re.search to re.sub and we'll use the same pattern there. And we put in our replacement pattern here. We'll just replace it with some hash marks and we are going to turn this if into just a print and we'll at the end of that, we'll put the end= blank like that and we don't need this anymore.
So now, we have re.sub and sub is the search and replace in the regular expression package in Python and the first argument is the regular expression pattern itself. The second argument is what to replace it with when it's found, and then the string to replace it from. Now in this case, this will go ahead and print the string even if it doesn't find any replacement. So it will print the string either way. So we'll get the entire file with just those lines replaced. So I'll save this and run it and we'll go ahead and maximize this and we can see these places where the hash marks are found. These are places where it found either Lenore or Nevermore.
So that's the simplest form of a search and replace in Python. Let's say though that we just want to print the lines where we actually found it and print those lines with that string replaced. So one easy way to do that is instead of putting the print here we can do a search here and get this results of the search with match, hence we use this search function, just how we learned to do it before. So search in line like that and then if match, because we only want those lines whether it's been a match, then we can do a simple string substitution, and print line and say end= blank like that and we can use the replace method in the line object itself.
And we have our result in match and it's in the group method and what we replace it with is the hash marks. So this is actually doing the search and replace in two separate steps. The search is being done using the regular expressions and the result is in the match object. We can test the match object to see if it's true, then it will have a match and then we can use that match in the string replace method to do the replacement.
So if we save this and run it, so now we get only those lines where the string was found and we have the replacement in those lines. So those are a couple of simple ways to use Python's regular expressions module to do search and replace.
Find answers to the most frequently asked questions about Python 3 Essential Training .
Here are the FAQs that matched your search "" :
Sorry, there are no matches for your search "" —to search again, type in another word or phrase and click search.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.