Demonstration- Worked Exercise 7.1

دوره: Python Data Structures / فصل: Chapter Seven- Files / درس 3

Demonstration- Worked Exercise 7.1

توضیح مختصر

I saved this so it's an empty file, and Python's perfectly happy running absolutely nothing because I guess you've made no mistakes. And so you see that by my action here in the browser, I have saved the file into the exact same folder that I've got the code for 7_1. There is a non-printing character at the end of every line called a newline which is the way in files we store the fact that it goes back to the beginning.

  • زمان مطالعه 9 دقیقه
  • سطح متوسط

دانلود اپلیکیشن «زوم»

این درس را می‌توانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید

دانلود اپلیکیشن «زوم»

فایل ویدیویی

متن انگلیسی درس

Hello and welcome to Python for Everybody. My name is Charles Severance, I’m your instructor. And in this particular video we are going to do exercise 7.1. And this honestly, even though it’s a really simple exercise, is one of my favorite exercises in the book. Because in chapters one through six we’ve been just kind of learning the basic mechanics. It’s like we just, it says Bonjour, and we say Bonjour. We’re learning but it’s not very fun because we’re not really solving it. So this is our first program that’s going to read a file and do something with it. And the only thing were going to do is we’re going to convert it to uppercase. So it’s just a really simple file, okay? So here we go. But the nice thing, so this is important though. I keep it simple because you’ve got to figure out how to manage files. And that’s the payoff for everything that we’ve done up till now. So let’s close that one and say File > New Folder, ex 07 01. Welcome to Files, here we are in Files. File > New File and I’m going to say File > Save As and put it in ex 07 01. And I’m going to call it, I could probably come up with a better naming convention than these things. But it works for me and I’m a programmer. So here we have this file. Let’s start up a shell, a terminal program, command line in Windows, shell in Unix, cd Desktop, cd Python for everybody folder in the desktop, py4e. cd ex 07 tab to get me to that and I’m in this working directory and I have these files in there and so I say, python3 ex 07. I saved this so it’s an empty file, and Python’s perfectly happy running absolutely nothing because I guess you’ve made no mistakes. Okay. Now here’s the key thing, we have in this situation we’ve got to have this file. And this version of the website has, this could also be but what I’m going to do is I’m going to Ctrl-click on this. Ctrl-click, Open Link in New Tab, okay? So now I have this new tab and this is the file, okay? And this is the file that we’re going to read. We’re going to read this file, okay? Lots of stuff in this file. You’re going to get to know this file really well. The key is, we got to put this file in this directory because this program is running in this directory and this program needs to open the file. And this is where if you got some magic little button and whatever, click run Python it doesn’t know what directory it’s running in. So we’ve got to get this file in a directory so that the program runs in the same directory, so it can open the file we’re looking at, right? So here’s the directory we want to go to. And I’m going to say, so I’ve got this file sitting here. And this works for text files once they’re sort of viewable in the browser. I’m going to say File > Save Page As and I’m going to go to my Desktop. Make this a little bigger, Python for everybody, ex 07 01. And you see this is sitting here and I’m just going to say Save. So that’s now been saved as if I’d downloaded the file. So let’s go into the terminal and do an ls. And so you see that by my action here in the browser, I have saved the file into the exact same folder that I’ve got the code for 7 1. So now I can open this file and I can make sense of this file, okay? So I’m going to Atom and you see Atom even sees the file. So now I can even open the file in Atom. And here’s the file in Atom. Atom knows how to read these text files and so away we go. So now ex This next few lines of code you are going to get to know pretty well. And so I’m going to create a variable called, come back I was in a wrong place. I’m going to open the file with the open command fh = open inbox-short.txt. Now remember that open does not actually read the file, it kind of gives us this little portal where we can take a look at the file. And so if I print fh you might expect it would contain all this data, but it doesn’t. It just is a file handle. So let me run that. python3 ex 07 Okay. So it knows some information, we’ll learn later what objects are. This is a Python object that has some information in it, but the information in this object is not actually the file data. To read the file, we are going to write a while loop. I mean a for loop, sorry, not a while loop. So we’re going to say for lx. Again, line is a good name here, but I want to use a non-mnemonic variable. for line in fh, and then I’m just going to say print lx. Okay? So this is going to loop through every line in the file and print it out. going to have to make this a little bigger now. because it’s going to be very chatty. So that is just a loop to read through. Maybe make this a little smaller. Oh boy, oh boy, oh boy, missing parentheses in call to print. What am I doing wrong here? Well, the problem is I’ve been using Python 2 for so, so long that when I’m not drinking enough coffee then I’m going to talk Python 2. So the right way to do this, let me just run this in Python, never mind, I won’t run it in Python 2, I’ll just fix it, because prints go in parentheses in Python 3. So I save it, and now let’s run it again. Okay, so there you go. Now as we scroll through this, you see right away the problem that, I’ll get rid of that print statement right there, let’s get rid of that guy, just delete him, we know how that line works. But you see this extra space, remember? Because in the file there is a newline, so if you go over here, let me make this wide as well. There is a non-printing character at the end of every line called a newline which is the way in files we store the fact that it goes back to the beginning. So it’s like character, character, character, character, character, go back to the beginning. That was a newline. Character, character, character, character, character, character, character, the next character’s a 3, the next character’s a newline. And so it goes to the beginning of the next line. So that’s the newline. And print, the print statement automatically adds a newline. And so in this case we have one newline from the file and then we have another newline that print’s adding, okay? But that’s okay. We can say ly = lx.strip. Actually, rstrip. Just strip the characters from the right-hand side, the non-printing characters from the right-hand side. Then we’ll print out ly. So I’ll clear my screen and run that again. So it does exactly the same thing but you don’t see all these blank lines. And so ly’s a different variable. I made a strip of it. And then I did printed ly instead of lx. If I had printed lx, I’d get these extra blank lines. And so, this is a very common thing. You open a file, you loop through all the lines in the file, and then you throw away the non-printing characters at the end of the line. And then you do something with it. So right now we’re just printing it. But what we’re supposed to in our assignment is make it uppercase. And so, let’s just call the upper method. Make them all uppercase, see if that works. And there you go. And so now the line has been shouted. There are only one line and the everything is fine, right? So it’s been shouted, all uppercased. This little syntax here is what is called a method. This is a string variable and upper is a method within a string variable that returns us an uppercase version of that. And again that’s object oriented terminology and we will learn about that in an upcoming chapter. But for now we just sort of type it and understand it and later we’ll understand better that there’s a whole series of things you can do after this dot for a variable in Python. And so there you have the exercise 7.1 for the book Python For Everybody. I hope you found this useful. See you on the net.

مشارکت کنندگان در این صفحه

تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.

🖊 شما نیز می‌توانید برای مشارکت در ترجمه‌ی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.