Check out my personal channel for fun engineering content! @peterlemaster8310
This video discusses the method for reading data into python from a text file. This includes step by step instructions for accessing the file from the same folder as the python program, then storing the data in a list, then filtering the special end line characters out and storing them in a modified list. There are numerous ways of filtering, processing and modifying the data read in and then you can perform any actions you could do with string variables with the data inside your python function once you learn how to read the data in!
If you have any questions or comments or want to see something specific in the future let us know about it with a comment!
We have hundreds of hours of instructional content on the channel and create more every week so be sure to subscribe to stay up to speed! Thanks for checking out the video and good luck with your projects!
what's going on you guys welcome back to
the channel and today's video is going
to be reading a text file into python
[Music]
so first thing uh when you read a text
file into python is you're going to need
your text file and the easiest way to do
this is to actually just have it in the
same folder that you have your python
code so if you
don't have if you do have your text file
but it's not in your python directory
then you should copy it and copy it into
this folder there is a way to read from
whatever directory you're in using like
a file join a file path join
but that's a little more complicated and
in depth than we're going to get in this
video
if that is something you want to see
maybe let me know about in the comments
below i can do that in a future video
but for now we're going to say that
you've got a copy of your text file
in the same folder as your python code
and that's just going to make everything
easier because we can direct address it
so i'm going to start by making a
variable called file and i'm just going
to use the python's built-in open
function and first things first you give
it the name of your file so you can tell
if you look at my system tree there's a
whole bunch of tutorials i've done and
then one text file and it's just called
my text file dot txt
and then the second thing you're going
to give it is
a command of what you want to do to that
text file
and for me i want to read from it so i
give it an r if you were going to write
to it you would give it a w but that's
not what we're doing in this video
and then to actually create a variable
where you have the data line by line
that's in that text file stored inside
another variable you're going to
just another variable i'm going to call
it read because you've read from it at
this point but you do file dot
and then read lines and so what this is
doing just so you can see it i'm going
to go ahead and print out that variable
we just made
it's creating a dictionary of each line
of text and so you can see this is
what's inside of my text file sample
text pete is cool you know one two three
four five
more sample text and then pete is cool
again so it's a total of five lines and
you can see it automatically is putting
this backslash n in there because inside
the text file every time you have a new
line python actually recognizes newline
as a character and so this is backslash
n if you've ever
seen like uh
wanting to print out a string and have
it on two lines you actually need that
backslash n but there is a way to remove
it in python so we'll do it two ways
we'll start by saying
for line inside of our
read dictionary
um and then we're going to check and see
if the
character in it that is the last
character
is equal to
the new line operator so backslash n
then what we're gonna do is create oops
one two main character we're gonna
create a new list that i'll call
modified
and we're going to
add just a second here so because we're
gonna
have a new version of this file
that's going to be a new dictionary just
like read is but we're going to remove
the backslash ends
we're going to need to create an empty
dictionary empty list sorry i said
dictionary a few times it's a list
we're going to need an empty
list at the start of our file so that
inside of here when we're appending to
it it already exists
and then what we're going to do
is we're going to add everything inside
of the line up to the last character so
if you haven't seen
my previous tutorial video on how to
modify a string i recommend you go back
and check that out because some of these
addressing of individual characters
inside of a string might not make sense
to you but that's not the purpose of
this video so just brief summary of what
we're doing we're checking if the
last character in the list that's what
this minus one address means and if the
last character inside of each string
is backslash n we're going to add the
the entire thing from start which is why
there's no variable no value before the
colon up to but not including the last
character so this is how we're going to
remove
the last character from the list
but then what we need to do because if
you see when we print it out before the
last entry does not have a backslash n
so we have to include an else statement
that's just going to say well now we've
checked and the last character is not
backslash n so we're just going to
append it
with the entire line so this should give
us the entire
list and we're just going to instead of
printing out read now we're going to
print modified
so let's go ahead and run that
let's go ahead and run that and there we
go so now you can see we have sample
text pete is cool one two three four
five more sample text but we don't have
those backslash ends
and it didn't mess with our last
statement
so that was the first way i wanted to
show just because this is how you would
do it if you needed to iterate through
each line of code and check for any
conditions
but in the specific case of the
backslash n
there's actually a much easier built-in
function which is just going to be
modified
dot append
i'm deleting instead of backspacing
modify.append
and then line but then you include this
dot strip function and so that's its
exact purpose is to remove that
character that you don't need
from each line so
let's see why am i getting the
modified dot append
okay
and so this modified dot append
line dot strip
is um just exactly what you need for
removing it
if i run this now
you're going to see we get the exact
same
thing as before
so just doing that dot strip
functionality allowed us to remove all
four of these rungs of code
and so that's pretty cool now let's do
one more thing in this video uh without
getting too long let's go ahead and
remove duplicates so you see this uh
string p is cool is in here twice
and what you could do is you could type
out that entire string and say you know
if this entire string is in there twice
like you could check each thing but the
easiest way to make sure you don't have
any duplicates is just add a statement
and say if line
not in
modified so what you're doing here is
you're saying
if the new line that i'm reading in
is not
already in the modified
list that we've created
then go ahead and add it
so
what it's seeing is if line is not in
modified then add the line
minus that last character of the
backslash n
and so you can see we only have p is
cool once now and if in our text file
this was just sample text instead of
more sample text this would not appear
twice as well okay so that's it for this
video hopefully you found it useful let
me know in the comments below if you
have any questions on anything you saw
here today or anything in particular you
want to see in the future and uh thanks
for checking out the channel and as
always good luck with your code thanks
bye