Complete Python Regular Expressions Masterclass! From Beginner to Advanced | Naser Jamal | Skillshare

Playback Speed


1.0x


  • 0.5x
  • 0.75x
  • 1x (Normal)
  • 1.25x
  • 1.5x
  • 1.75x
  • 2x

Complete Python Regular Expressions Masterclass! From Beginner to Advanced

teacher avatar Naser Jamal, Computer Engineer

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Watch this class and thousands more

Get unlimited access to every class
Taught by industry leaders & working professionals
Topics include illustration, design, photography, and more

Lessons in This Class

    • 1.

      Why Regular Expressions?

      2:31

    • 2.

      Introduction - Verify Users Email Using Regex

      6:23

    • 3.

      Methods to Search for Matches

      3:42

    • 4.

      Methods for Match Objects

      3:30

    • 5.

      Extracting Useful Data

      16:22

    • 6.

      Sorting Unordered Data File

      12:53

  • --
  • Beginner level
  • Intermediate level
  • Advanced level
  • All levels

Community Generated

The level is determined by a majority opinion of students who have reviewed this class. The teacher's recommendation is shown until at least 5 student responses are collected.

92

Students

--

Projects

About This Class

In this Python Class, we'll be learning all about Regular Expressions (Regex) in Python.

Regular expressions are not as hard to master as they seem at first, so buckle up for a 100% hands-on learning journey!

What you'll learn:

  • You will learn the Fundamentals of Regular Expressions in Python from the ground up.
  • You will go from Beginner to Advanced in pattern matching abilities in Python.
  • You will be able to apply those skills to solve real-world problems.

Why would you take this class?

  • Do you wanna be able to automate tasks that would take hours in a matter of seconds?
  • Do you want to elevate your Python programming skills without spending too much time?

  • Have you ever been confused by the Regular Expressions topic and wanted a detailed step-by-step learning guide?

  • Do you want to be able to do basic to advanced pattern matching tasks using Python's incredible power?

Requirements

  • No programming knowledge is required.
  • No need to Install python, you could use Online Python Compilers.
  • Windows / Linux / MacOS operating system.


Regular expressions can be used for pattern search, from validating emails to matching any sequence of a string. It's a very powerful tool for matching text patterns. This Class covers all your needs to have a complete understanding of regular expression! We'll learn all of that through step-by-step process!

Meet Your Teacher

Teacher Profile Image

Naser Jamal

Computer Engineer

Teacher

Hello, I'm Naser.

I try to simplify complicated programming topics and present them into short and bite-sized videos!

See full profile

Level: All Levels

Class Ratings

Expectations Met?
    Exceeded!
  • 0%
  • Yes
  • 0%
  • Somewhat
  • 0%
  • Not really
  • 0%

Why Join Skillshare?

Take award-winning Skillshare Original Classes

Each class has short lessons, hands-on projects

Your membership supports Skillshare teachers

Learn From Anywhere

Take classes on the go with the Skillshare app. Stream or download to watch on the plane, the subway, or wherever you learn best.

Transcripts

1. Why Regular Expressions?: Hello and welcome to the complete Python regular expressions masterclass. This class is fully updated and interactive with hands-on learning to help guide you from 0 to hero and learning Python RegEx. Why regular expressions? Python is one of the fastest-growing, most popular programming languages in the world, and it is projected to grow even more in the future. So if you are building your career as a programmer or a data scientist and you want to set yourself apart from the rest of the crowd learning only one technical skill or that Scalar's regular expression. So what can you do with regular expressions? You can automate tasks that will take out in a matter of seconds. You can do advanced button matching tasks using Python's incredible power, you can read, salt and rewrite data to your own needs. And the best thing about it is that it's so easy and straightforward within a few lines of code, you will be able to do tasks that would take hours to do manually. And now let's run our program and see how it looks like. This looks so much more readable and much easier to work with. Let's open the first file next to it and combine them together. The difference is day and night. We did that. And what 15 lines of code minus the spacing. This would have took a long time to sort it manually. And there's also the possibility of human error, which could run the entire database for us. Instead, with few lines of code, we let the machine do the work and eliminate the possibility of missing a value of two and creating a snowball effect that runs our data. So who is this course for? Beginners who have never programmed programmers coming from different languages. Programmers are no basic Python, but want to elevate their skills. So what's the actual costs like? Let's take a look first. I will give you a brief introduction on how to use drug X into your Python program. Then we will write our first pattern to verify user's email using RegEx. Then we will take a look at different search methods and match objects and showcase how they work by solving problems. Then we will learn how to extract useful data and we will use a tool that would make belting patterns much, much easier. Finally, we will have a real-world exercise where we will be given a file asked to read into it, sorted values in a specific order, then write the new sorted list and a different file. We will go through all of this and more in a timely manner. This class is meant to be short, concise, and straight to the point. You could waste less time in theory and more time in hands-on experience. So what are you waiting for? It's time for you to take your programming skills to the next level, enroll today, and I'll see you inside the course. 2. Introduction - Verify Users Email Using Regex: Hello and welcome back to my video. Today we will be taking a look at regular expressions. Regular expressions or regex can be used to check if a string contains a specified pattern. It's a powerful tool to find patterns in a text. So let's take a look. So say for example, we want to verify user's email using RegEx. The first thing we have to do to start using RegEx is to import the RE module. Now assume this is the email address we want to verify. Now we will create a button and set the rules for that button we want to check. Now we have to split the e-mail address into four parts to have a better understanding for the first part, which is receiving snap or users ID, it can contain a to Z and 0 to nine. The beginning we use the carrot or the circumflex symbol to initialize that. We want to match a string which starts with the following. And to end this pattern, we will use the dollar symbol. But of course we want to add more condition store strings. You're not going to end it right now, the second thing we type this a to Z and 0 to nine. This means that this part of the string can contain any letter in any number. Now we will type a plus symbol indicating that this can occur one or more times. The recipient's name can also include symbols such as period, underscore and dash. Period underscore and dash. We use the backslash to define our special characters, period, underscore, and dash. We use the question mark here because those symbols can be used either 0 or one at most if they occur twice, that means it's an invalid email. That's why we had to specify that with a question mark symbol. Now, after those symbols, you can also have any characters from a to Z and 0 to nine. After the symbols we have specified, the period underscore and dash, we can have letters from a to Z or numbers from 0 to nine. Just like this. Again, we will use the plus symbol because they could occur more than once. Now we have to write the add symbol, which occurs here. Then after the add symbol, we will have things like At Hotmail or Gmail and so on and so forth. So it can be any alphanumerical characters. Then after the ad Hotmail comes the period. So we have to specify that. Now after the period comes things like.com.net. Usually they are length between two to three characters. So we have to specify that. Now we will use the dollar symbol to end our string. Now that our pattern has done, we want to receive input from the user by prompting them to enter their email. Now we will create an F loop to check the button would they use us? And so the re.search method will take two arguments. The first one being the button we created. The second one is the user's input or the string we want to check. So let's run our program and see how it looks like. So let's enter this same e-mail address. So I believe the mistake I did was leaving a space here. And when I remove the space, it works just fine. A high type phonetic instead of genetic map. So let's see if we entered an invalid email on purpose, like generic. And then I used another simple and valid e-mail. Well, from the looks of it, our program works just fine. So let's take a look at another example. Say we want to look for a certain button on the following string. So it appears that capital a was repeated twice. And we want to check for that pattern in our distinct. To do that first we'll define our button. Here. We will be using the compile method. We could also use the search method like we did in the previous example. So R here before our string means raw string, python, raw string treats backslash as a literal character. This is useful when we have a string that contains backslash n. Don't want it to be treated as an escape character. So let's see the difference between a regular strings and raw strings. So the first one, which is a regular string, treated the backslash n as an escape character. In this case, it's used is to start a new line. And as you can see it, however, in the second string, which is a raw string, it's ignored the users of backslash slashing and treated it as a literal character. Similarly, if we replaced backslash n with backslash x, we will have an arrow and the regular string because backslash x does not have a purpose. However, our raw string, we'll ignore that and printed just as is. So let's go back to our test string. Now we will create an object. So we type the button we will be using. Now we will use the find method. Inside defined ETL method, we will type our tests shrink. Now we have created an object called matches that we can iterate over using enough flow. As you can see, it shows two occurrences of a while showing their position. Also, it is case-sensitive, meaning it didn't pick up the lowercase a. We can also make our code shorter by using r e, find Eta right away. For example. Now we will type the button we want to look for, which is a0. And then the second argument we will pass the string we want to test. As you can see, it returns the same results with less lines of code. So that's it for today's video. Thank you so much for watching and I hope to see you in the next one. 3. Methods to Search for Matches: Hello and welcome back to my video. Today we'll be taking a look at methods to search for matches. So in the last video we worked on this example, which used find better. There are other methods to search for matches, such as match, search and find all. So find it to return an object with the position of the string we were looking for. Find all however, only returns a string, for example. So that's what find all. What's about the match method. The match method looks for button only in the beginning of the string and returns only the first occurrence, for example. So as you can see, it didn't return the second occurrence of a. Now let's change the button. We want to look for it for something else other than a, for example, as you can see, it's return none. So even though five-ninths eight has occurred in our string, but because it wasn't in the beginning, it's return none. So what about search? Search works very similar to find better. However, it's only returns the first occurrence of the pattern, for example. So the four most common match methods, or find a better search, find all and match. So let's take a look at this example. We have the following string which says $3 for the shirt, we wouldn't use RegEx to check if the string starts with a digit. In this case, it's three. And if that's the case, we will print that digit. So first let's import RE. And then we'll create a string and put this text inside of it. Now we want to create a button that we want to look for. And we're gonna be using raw strings. As usual, we're going to type in our button backslash t. Backslash d means one digit, which is exactly what we have here. So now that we created a button, let's create a match object. Here we would pass in our button, then we will pass in our string. Now we can simply say F, which means if S is true, then prints the following. You might ask, quote is matched dot group, group is a method of the match object. And what it simply does is that it returns the matched string, which in this case is three. We'll be taking a closer look at Match methods in the next video. But for now, group will return the number three for us. So let's run our program and see what it looks like. So even if we change this number to five, it will return five. So if we don't use the group method, this is what we're gonna get. You're gonna get the full match object. We only wanted the string five. That's why we use the dot group method. So that's it for today's video. Thank you so much for watching and I hope to see you in the next one. 4. Methods for Match Objects: Hello and welcome back to my video. So in the last video we saw how find itself with the return an object that we could iterate over using enough loop. So this object itself match has its own methods and we're going to be taking a look at them. Those are the methods start and grow and span. So as you can see when we print the result, it returns this object. We can only print the span over here using span for that, we're going to use Match dot span. Now when we run our program, we're gonna get a tuple containing our span that this can be useful if we want to print out exactly where our pattern occurred. For example, as you can see, this looks more readable to the user. If we print out the type of this match dot span method, we will find out that it's just a table that includes the position, what our match has occurred. We can also print the start and end position of our string using start and end. So let's take a look. Match, then, comma match. So to return the integers of our starting and ending position, we could do something like this. And when we print out its type, will find out that it's just an integer. Finally, if we only want to print the string that will match, we will use a group. And as expected, when we print its type, we come to know that it's a string. So in conclusion, matches an object that contains the span in which the match slash pattern has occurred. And it also contains the string that was matched. We can access individual components of this match using the methods we went through the start method to get an integer that starting position in which the pattern started. Method to get an integer, the ending position in which the button ended. Group method to get the matching string. And finally, the span method to get a table which includes the start and end position of the button. So that's it for the basics. And the next video we'll be taking a look at how we could extract useful data using red X for data science or documentation purposes. So that's it for today's video. Thank you so much for watching and I hope to see you in the next one. 5. Extracting Useful Data: Hello and welcome back to my video. Today we will be learning how to extract useful data and elevate our regular expression skills to a whole new level. In this video, we will be using a tool or a website called beg x, one-on-one. It's a very powerful tool that will not only teach us more about RegEx, but also simplify the process and visualize it follows. First, we will click on bifan as our programming language that we're working with. Now assume we have the following string. So taking a look at the string, we have two numbers. One is a phone number and the other is a two digit number of the company sells right off the bat. What we know about phone numbers is that they consist of ten digits, so that easily differentiates it from the other number over here. So we can set our RegEx to extract whenever it finds a ten digit sequence number. So when we go over to the quick reference tab, we could find the syntax we need in case we forget it. So here it says backslash d means any digit, or in other words, any number. So let's go ahead and use that. As you can see when, when we type backslash d, Those are the matches we get. It essentially matched every digit we have. Now what we want to match two consecutive digits we can tie to backslash days. Now it matches two digits. So before we had 12 matches, now we have six. Because it doesn't match each individual digit now it only matches two digits together as one. And if we type three backslash these, we only going to get two matches of three consecutive digits. And as you can see, 20 million isn't matched because it's a two digit number, and so on and so forth. So one could think when Izzy, if a phone number that's ten digits, we could just stay ten backslash these two match that sequence. Technically, they're not wrong and that would work just fine, for example. So we have 345678910. So when we look at the matches, this is what we're going to get. Obviously, there are more sophisticated ways you could do that. For example, you could go to the quick reference tab. Common tokens will find an expression that says exactly three of a, meaning, exactly three occurrences of the letter a consecutively. So let's try and use it to our advantage. So right off the bat, we don't need a, we want backslash d instead. So we type backslash d and then between two curly brackets will enter ten instead of three. Now, this gave us the same exact results with much less coding. Of course, some might say, Well, phone numbers, it will be presented in different format, for example. So if we want to extract or phone numbers and they're clearly in two different formats. We could do the following. First, let's save our expression over here and we'll come back to it later. So one of the bat, our button that we want to match starts with a bracket. However, if we type of packet until a regular expression, we would run into a problem. And that is because the bracket is a special character and it has a meaning for our program. As you can see when we look at the quick reference step, we find that bracket is used to capture everything in closed bracket, in this case is called a special character. So to use a special character, we have to escape it. And how do we escape it? We simply type a backslash before it. For example. Now as you can see, this works fine and it detected our pocket over here. So let's continue. So we have three digits after the bracket, let's die. But in our button, three digits. And as you can see, it detected the number. So, so far so good. And again we have a bracket, but we can type the brackets right away. We have to escape it using backslash. Now we have a dash symbol. Then we have three digits, and then a dash simple, and finally four digits. Now, as you can see, it's matching our phone number format. So we're done with this expression, but we still have our first expression which is backslash d ten. How do we tell our program to look for this button or this pattern simply, we could use the vertical bar symbol. So when we type in the search over here, or we get this alternate match, either a or B. So let's use the simple. Then we will paste our expression over here. And as you can see, detected both of them. So it says either look for this pattern or this button. So if we want to use this at our Python program, we could do the following. So first we import our eight use drug X. Then we make a string to contain our text. Now we create a baton. And we call the best the button we created. Now we will use find all will first type our pattern. Then the text we want to match. Now we simply print match. Let's look at the results. As you can see, it works just fine. So we can always use RegEx one-on-one to build our expression and test it before we come to apply it here on Python, it makes things much easier because it visualizes the process for us and shows us what's being matched in real time. So now for level two, we have this giant textblock and we want to extract the titles from it. So the titles here are our purpose and our opportunity. And we want to extract dose. So to start off, we can type note because they start with naught. And then there is a space, then a number. Now we captured note one and n2, but we still want to capture the text that comes after it. If you look closely, you will see that the title ends with a new line being created here and here. And we know that a new line means backslash in Python. We want to capture all the texts until we encounter a new line. The question remains, how do we type that in regular expression? So if we look over here to the quick reference stop, it says any character except a, B, or C. So let's open a new doc x start to experiment with. So say we have the following text and we want to say match all characters except the percentage sign, the equal sign. So we will type the following. As you can see, it matched everything except those samples. And in order to do a sequence, we add a plus sign at the end of our expression. Now it says one or more characters up until you encounter one of those signs. So this is what the plus sign means. One or more occurrences of a, any character that you specify. Now going back to our texts, let's apply what we learned. Okay, so now we have a number, then we have a space. So we'll add a space. Then we will try this expression. Anything except a new line. It only matches the first character and stops. Instead, we wanted to capture one or more characters. For that, we will use the plus sign. And when we go to the quick reference type and type of one or more, we can use the plus sign for it. We could also use the star sign, which means 0 or more occurrences. And it would give us the same results. Now when we go to the match information, we can see that it works, but we're still not done because we want to capture the titles and not one and node two are not part of our titles. For that we will use brackets. As you can see, it says capture everything enclosed. So when we go to our expression, this is exactly the expression where it started capturing the title here. So let's put brackets around that expression. Now what it does is that it, groups are captured those results for us. Now what happens is that it matches those other results like node one and node two. So let's use that in our Python code. And to copy paste this giant block of text, we're going to use three single quotation marks to account for backslash n or new lines in our text. So instead of this old method, going to be using three quotation marks, 123, then we're going to base our text, and then we'll use another three quotation marks. Now let's copy our regular expression that we wrote and based it at pattern. As you can see when we run the program, it only returns the title is for us without not one, not two. And now for level three, we have a relatively small textblock. However, we will extract a lot of information of it. So if we take a look at the useful information here, we can see that the earnings were provided in the following format. Fiscal year FY then followed by the year, in this case, it's 2021 or 2020, and then a space and then the courtroom that year. In this case, this is Q1 and this is q4. So we know in a year there can only be four quarters, Q1, Q2, Q3, and Q4. There cannot be a Q5. So we'll keep that in mind. So right off the bat, our pattern starts with FY. So let's type in FY. Then obviously there are four digits, which in this case it's the year. And how do we do that? We use backslash d. And then between the curly brackets, we type four. So this expression means f y followed by any four digits or any phone numbers. So far so good. And then there is a space after, before the Q1 and Q4. We have to specify that in our regular expression. So let's type a space. Then there is a queue. So now the queue is followed by a digit. However, we cannot just type slash d because that would include things like which in this case is wrong. So we want to make sure our digits are from one to four. How do we do that? If we look at the quick reference tab, there's this option that says a single character of a, B, or C. You can explicitly specify the numbers you want to include, which is 1234. But there is a better approach. We could use this option in the quick reference stamp that says a character in the range a to Z. Now we could type one to four instead, and it would give us the same results with less coding. So now let's test our pattern and Python, we call me I love text and base it on this thing name text. And then we copy our pattern and based it in the pattern, our Python program. And now let's run the program to see the results. So what if one of the flights was lowercase, but we still want to match that anyways, we could do the following. Lags equals ignore case. Now when we print the results, as you can see, it's matched the lowercase f, y. And let's say we don't want to include the FY, which stands for fiscal year, Simply, we could use the brackets to capture and retain only the parts we want. So for example, now when we run our program, we can see it, it only captured the parts with specified inside of the brackets. So let's go back to RegEx one-on-one. We will able to match the year and their quarters. However, we still want to match the earnings of that set here. And assume we had other numbers and our texts that we want to avoid, for example. So let's save our expression here. We don't want to capture this number. We only want to capture the numbers that start with a dollar sign. However, we can't just type the dollar sign as it's a special character. That means end of a string, as you can see in the quick reference tab at the bottom. So because it's a special character, we know we have to use the backslash to escape that special character before using it. Now It's matches what we want. Now we have a digit after that, so we can type backslash d. However, there is a decimal point after that, so instead we will use square brackets. Then we have a decimal point because it's a special character, as you can see in the quick reference stop. We have to use the backslash before using it. Now we close our square bracket. Now we need to put a plus, which means one or more occurrences. Now, if for some reason the company's earnings became 10.4 billion, it still counts to two digits before the decimal point because we specified that using the plus sign. Now let's paste this regular expression and our Python program and see if it works. Now we'll take the button. Let's remove this. Now let's run our program. And of course, if we don't want the dollar sign, we could always use the brackets to capture what we want. Say we want to match the year, the quarter, and the earnings of that here. How do we do that using Drug X? Well, we already made a regular expression to match the year and the quarter. So let's use that first. Now, after the quarter comes, any character before we reach the dollar sign. So we'll type any character except the dollar sign. And now we need to add a plus sign to capture one or more occurrences. Now it's matched anything up until it's countered, a dollar sign. So that's what we want. Now we'll go back to the regular expression we created earlier to match the Annex and paste it into our expression. Now as you can see, it's matched exactly what we want. But one might say, well, we don't necessarily want the texts in between that says was, and it was simply you can use the brackets to capture what we want exactly. Now, it only captures those results. So let's go back to our Python programs you how it looks like. As you can see, it's working as intended. And of course, if you don't include the dollar sign, we could just include it within our captured expression. Look at type, for example, earnings presented in billions. So that's it for today's video. Thank you so much for watching and I hope to see you in the next one. 6. Sorting Unordered Data File: Hello and welcome back to my video. Today we'll be taking a look at how we can sort an unsorted text file using RegEx. So this is the file we're gonna be taking a look at. The filename is data list. I have it saved on my desktop. So when we open the file, we have three different types of data, name, email, and phone numbers in this list. The FirstName here belongs to the first email here, and belongs to the first number here. Similarly, the second name belongs to the second email and to the second number, and so on and so forth. Now the problem is they're not listed next to each other wound. I have those values next to each other one-by-one. Obviously, if you would do this manually and place the first name next to the first email in the first number, it would take you forever. So how do we structure this list using few lines of code? So let's head into our Python program and take a look to start using RegEx. The first thing we wanna do is import our array. Then we want to open this file. If you want a more detailed explanation of how to handle files in Python, you could check my class on File Handling. So this is how we open a file in Python. This is how you would open a file if it was saved in the same directory as your Python file. However, our file is saved on our desktop. We have to specify the location in our program to know the location of our text file, we'll just head to the location where it's located, then right-click Properties. And here's the location, will just copy paste it in our Python program. Now we have to change those backslashes, two forward slashes. As you can see now it's not showing any adults. Now we are reading this file and saving its contents to this variable called contents. The next thing we wanna do is to create patterns to match the data we have. Luckily, we've already created buttons to match emails and phone numbers, so we'll just use those since they work. Those are the same ones we used in the previous classes. So this is the button that would match emails, and this is the button that would match phone numbers. However, there is a slight difference. If you look at the numbers, they are similar to ours, in which they start with parentheses, then there's three numbers, but instead of a dash here, they have a space. So let's replace this dashed by space. And then they have three numbers. We have three numbers and then a dash. And then for numbers. So this should match the numbers in our list, just fine. So those two buttons should be working just fine. Now, one more button we need to create is the name pattern to match the names in the list we haven't created that. We created now from scratch. So if we look at the list names, essentially what we have is any capital letter from a to Z, and then any lowercase letter from a to Z, then a space. Then again, any capital letter from a to Z, and then any lowercase letter from I2C. So this seems pretty straightforward. Let's do it. So I'm Doug x terms, this is how it looks like. So this button is separated into two parts, the firstName and lastName. And they're separated by a space in-between them. The firstName is any letter from capital a to capital Z, but only occurring once, then any letter from lowercase a to z. This time we added a plus sign because it could occur one or more times. So if we look at our list, we only have one capital letter here and then multiple lowercase letters. And this is what our buttons says. One capital letter, and then any number of lowercase letters. Then we have a space in between them. And then the same button again, one capital letter, and then any number of lowercase letters. So now we have our patterns ready. Let's choose a search method. We could use find all, or we could use find editor. But I think find S that is more suitable in this case. So let's create a fine debtor for each vowel patterns. So here we created a fund. It's an object named emails, gave it the button that matches emails. And then specify the string we want to match it with. In this case, our string or our texts is stored in this variable called contents, which leads from our text file. Same goes with phones and names. So if we were to use find editor like we usually do, this is the results we're going to get. So this is how we usually use find better. Here's the object we want to iterate over and we say for each item and emails print each item. So let's run our program and see how it looks like. As you can see it to the tens, the full object, we only need the string or the matched e-mail. For that, we're going to use dots group method. Let's run our program. Now it's only returning the strings. But we're still not done. This is essentially given us the same file we already have where the emails are listed in one place and the names are listed in another. How do we list them next to each other? You might suggest we could create another for loop for the phones and names. So for example, let's just copy paste this phone. Phones here. Sorry. And then same for names, for name names. Then we place name here. So let's run it and see how it looks like. It. Essentially, it looks exactly like the same file that we have that doesn't work. Either. One might suggest place those four lobes underneath each other maybe. So if we remove the space here, then highlighted and press Tab, and remove the space highlighted and press Stop. Okay, so now inside of each other, Let's see how it looks like. Now it's even worse because it's printed the first email than the phone number and then the names and it's just a mess. But don't worry, those are specific tool for that. So essentially what we wanna do is iterate E-mail, phone, and name. At the same time, we don't want to iterate emails by themselves than fonts by themselves, the names by themselves. We want to iterate them all in the same for-loop. For that, we're going to use ETL tools to use Editor Tools. First we have to import it, don't worry, we don't have to install anything. It comes by default with Python. As you can see, it's suggested that told me, now let's zoom out a little bit. Close this. Okay, now that we imported it, this is how we're going to use it. So we're going to type for name because we want to iterate over name. And we also want to iterate over e-mail. And we also want to iterate over font. And then we're going to type gzip. And then include the objects we're going to iterate over. Oh my God, what's going on? So names, emails, phones. Oh, sorry. So for name in names and for email, in emails and for fun and phones. So this works just like a regular for loop for us, except now we can iterate over multiple objects. So now let's print our results. Print name dot group, and then emailed us group. Then finally found that. So it's very similar to the way we would iterate over an object using a regular for loop. Only thing to remember is to include all of those objects we want to iterate over inside of the zip parenthesis. Now let's run our program and see how it looks like. That looks beautiful exactly how we wanted it to be. But again, we're still not done. We want to write those results into a file. Instead of just printing them here in the terminal. For that, we're going to open a new text file in write mode, and let's name it sorted data. So f equals open. Then here we're going to type the name of the file. You're going to name it sorted TXT, and then a comma. And here we specify the mode. We're going to use the W mode, which means bytes motor. Now yes, it says Write mode, but this open function will create a file for us if it didn't exist, and if it towards to exist, it will overwrite the contents of that object. So if we leave it as is, this is going to create the file here in our workspace. We're going to create it in our desktop next door, or original file for that, let's specify the same location our file is located. So we're going to cover this dislocation and paste it here. Now let's we open this file, we will now write into it. So let's change our for loop to write instead of print. So we're going to type F, which is the name of the file, dots, right? And that's it. Now, we might get an error here. That is because the write method only takes one parameter, but we placed here three parameters. So let's place them in an f string. So f, then we're going to start a string. And that string, we're going to place those variables named dots, cool. Then e-mail the group. Then finally found its group. Then we're going to end our string with another double quotation mark. Now we should be set, but before we run our program, Let's not forget to close our file. So we're going to type def, which is the name of our file. So let's run our program and see how it looks like. We should see a file created in our desktop. Because there's nothing that determinant because we didn't print anything. But if you noticed here, it says sorted data, this is the file we created. Let's open it and take a look. It's kinda sorted the data for us, but not enough. We need to place a new line after we print the name, e-mail and phone. So here, after we written name, email and phone to create a new line after that. And now let's run our program. And don't worry if we run our program again, it's not going to create a new file. Instead, it's going to overwrite our existing file. So let's run the program and take a look at our file. Notice that our commas separating those values, that is because we kept them in our f string, as you can see. So if you prefer not to have them, you could remove those commas and your program will run just fine. However, I think it's easier to work with data when they are separated with commas. Optionally, we could label our data. So here we're going to type name, then a colon, then a colon, then finally on, and then a colon. And now let's run our program and see how it looks like. This looks so much more readable and much easier to work with. Let's open the first file next to it and compare them together. The difference is day and night. And we did that. And what 15 lines of code minus the spacing. This would have took a long time to sort it manually. And there's also the possibility of human error, which could run the entire database for us. Instead, with few lines of code, we let the machine do the work and eliminate the possibility of missing a value or two and creating a snowball effect that runs our data. So let's recap our code we imported or a drug X and editor tools to iterate over multiple objects. We then open the file we're going to read the data from and specify its location and opened it as f. Then we created a variable and we typed contents equals F, F, which is our file, the treat. So this contents variable reads our text file. Then we created a pattern for our e-mail, phone and name. Then we created an emails find at an object and gave it the pattern that it uses. So the emails uses the email button and we matched it with contents, which includes the data and our textFile. Same goes for fonts and for names. Then we created a new file and open it in W mode or write mode and name it sorted data. And then we created a for loop that iterates over three objects, names, emails and phones. And then we label them as we write them in our text file to have an easier time letting them. Then after we written in our file, we closed it. So that's it for today's video. Thank you so much for watching and I hope to see you in the next one.