Hi, in this video will be filtering out file names, so for this example, I have this file folder
in my working directory.
So I invite you to create a similar folder named files.
And inside that folder, please put these text files.
You can find these files attached in the intellectual resources and upload them in your files directory.
So let's suppose that these are bills pointing to different deeds or so, but the naming of these files
is not that good.
As you see, the personal name them did not use the best practices to name files, but that is why regular
expressions exist.
So our goal here is to filter only those bills from 1st of November on to the 20th of November.
That means we want to extract this first file.
Nov 13 The 2nd Nov 12 Nov 22 Not this one, because this is after November 20th, then we want to see
this as well.
November 14th.
Not this, not October.
So only the first one of the second and the third one.
Let's see how we can do that before we write the regular expression.
We want to get a list of these files.
For that, we want to use.
The path.
Class of the past Liberal Library, if you don't know what this is, you need to go back to the calls
and take the file and folder operations sections.
There I introduce you to the path of the library.
So then we want to create a root directory, which points to the folder where the files are located.
So files is the name of the folder.
Once we have that, we can create a list.
File names, a single two routes do those eight to do.
And let's print out financier converted into a list because it's a generator objects.
That is the lease or file paths.
There's a problem, though, these.
Items are not strings.
These are Paul 02:06 object types.
So we want to convert them into strings.
We can use a list comprehension for that.
File names as a new variable SDR.
So to denote that these items of this list will be strings that is equal to a list comprehension which
would go like file name, dot name from each postfix path, we extracted the name, which will give
us only that part for file name in file names.
That's one.
And let's print out file names, SDR now we're going to get on them to this here.
That is because file names at this point of the execution is empty because a sense file names was a
generator objects.
That means when we converted into a list, when we converted the generator into a list that means we
use the generators of the generator is exhausted.
Therefore, a solution is to get to this list comprehension and also the variable called here and just
place it in here.
So now we generate a generator again in here and then we don't print it out, so we don't exhausted,
but we use it in here in this list comprehension.
This time we will get this list.
So novelist, a story in this valuable and we can use it further.
So let's now write for a regular expression.
So we want to find some bills from November the 1st to November the 20th.
That means our files will always have this no port, no some files will have a capital and so uppercase
letter we can ignore case sensitivity using R e tort e kunwar keys as a second argument.
So that means both north with a capital letter and north with lowercase letters will be included in
the match.
Sometimes, though, we have November and sometimes we have just north.
So what we could do is we could just write a range here from A to Z and we could use a metal character.
So zero or more times the asterisk.
Like that.
Then after that, we have a dash, right, so it could be no Dash or November dash.
Then the next character could be one or two because we are filtering out only from the 1st of November
until the 28th of November.
So it could be no if one of two or three of four or five six seven eight nine, 10, 11, 12 and so
on.
So the next character is a number.
And so we use these parentheses, so we have to use them or operators inside these like that.
And we said the first number could be first from one to nine.
So one, two nine range.
So we want to work on every 10 digits whenever you want to feel two digits or numbers with regular expressions.
So let's say one two six seven.
You want to treat first the range from one to nine.
So one digits, then you want to treat the next range from 10 to 19.
Then the next.
So that's what we're doing here.
First, we work on the first range of numbers, so one digits from one to nine.
We see that or it could be 10, 11, 12 until 19.
So it could be one, followed by zero to nine.
So one zero one one one two one three one four and two one nine.
Then again, the vertical bar, or 20.
So just to see if you had to go up to 23rd of November, then you do something like two of them in square
brackets, you'd insert the range zero to three.
So that would include 20, 21, 22 and 23.
This is not the case.
We just want 20.
So 20 is fine.
Then after that, after the number, we have four dots and the tricky part.
So I executed that, but we haven't extracted the actual matches yet, so I shouldn't have executed
it.
So let's add the matches.
Matches are.
Hmm.
Now we have a list here, so be careful because previously we were working on text strings, but this
is at least steroids of your of.
The IP addresses were coming as Texas wants single string.
So in this case, we want to work on a list comprehension.
We would see something like file name for file name in file names, a city or so we go through that
list.
But if pattern that finds full file name.
Matches, I'll explain you, that's a list comprehension in just a bit.
So that's the ultimate Nov 12, Nov 13 November 14th.
No, there's not 20 here.
So let's try to add a 20 in here in these files.
So create a new file and let's say, Nov 20, the 16.
Go back to the notebook and executed the first cell where we get the files from the Working Directorate
execute that.
So you've seen enough 20s there now, execute the other sell as well, and we get enough 20, so it's
working well.
But let me explain you how this works.
So we are iterating over the file names.
Trim lists this list here.
File name for file name in five-string and now we will includes in the matches list only if this evaluates
to true.
So this evaluates to true only when the pattern is formed in the file name in the current file name
of the iteration.
So we iterate over these.
So let's say first, we have this as the file name variable inside this list comprehension and we see
is this pattern in that file name.
So if it is, then that's file name will be included in the list, so it will be added to the matches
list.
Otherwise, it will not be added to that list.
So we will end up with a matches list, which will contain only those file names which.
Satisfy this condition here, and that concludes this video on filtering data that contain numbers,
so thank you again, and I'll talk to you in the following videos.