Worked Exercise- 6.5
And so the basic idea is we're going to see strings of various kinds, and various lengths and we're going to want to extract pieces of them, okay? A powerful tool that lets you sort of save a lot of keystrokes, etc, etc. The colon is in position 18, the string we pulled out is blank 0 8 4 7 5, and the floating point number is 0.8475, so we've sort of solved this.
- زمان مطالعه 8 دقیقه
- سطح خیلی سخت
دانلود اپلیکیشن «زوم»
این درس را میتوانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید
متن انگلیسی درس
Hello and welcome to Python for Everybody. My name is Charles Severance, and I’m the author of the book and the teacher of this class. In this particular session we are going to do Exercise 6.5 from the textbook. It’s an exercise in parsing text strings. And so the basic idea is we’re going to see strings of various kinds, and various lengths and we’re going to want to extract pieces of them, okay? And so the idea is to somehow get this part out, and then convert it to a floating point number. This is a proxy for later things, where we’re actually reading files or reading stuff off the Internet, but parsing strings is an important thing for us to do. Okay? And so let’s take a look at a couple of different ways to do this. So let’s go ahead and get started, let’s go bring up our Atom and I’ve got it open nicely to the right spot here, and I’m going to make a new folder. Hopefully by now you’re finding Atom ex 06 05. Atom, or whatever your programmer editor, is sort of a powerful tool. I’ll close this one. File > New File. A powerful tool that lets you sort of save a lot of keystrokes, etc, etc. print(‘Exercise 6.5’), just for yucks. And this file, Save As. And again, until I save it, it’s not going to have the pretty colors. I’m going to save it in 06 05 ex 06 05.py And now it has the pretty colors, and here I am. Now, I’ve been doing these, and so now I’m actually already in a directory, so let me show you how to do relative directory. So I’m in this path right here. And I can use both in Windows and in Mac and in Linux. I can use the command cd.., that sort of thinks of the one before. The one that came before. And so now I’m up one directory. And if I do an ls, I will see that this new ex 06 05 that I just created in this directory from Atom is already there, cd ex 06 05. In the next chapter, we will be talking about files. And this is where you really need to know this concept of folders and files. So ls, and I’m going to run python3 ex 06 05.py and there we go, exercise 6.5. So we’re sort of in the right spot, we’ve got this going, and we’ve got this going. Pretty soon we’ll be putting stuff in the directories that need to be there and you’ll see how all that’ll work in a second. Well, in the next chapter where we’ve got to know all this stuff. Okay, so we’ll just grab you, this first line here. And paste that in. print(str) So. That’s right, and there we go. Actually, there’s supposed to be a space right there. So I don’t know why this space didn’t get copied and pasted from my copy and paste. So I’m going to put that space in. There’s supposed to be a space right there, I think, but we’ll see. So the key thing is if you look at the lectures from this section, you can like look for things, and you look for a pattern. And so what I’m going to do is I am going to look for a pattern that says find me a colon. Okay? And I’m going to say where is there colon equals str.find? I’m going to print out ipos. So I’m going to say, where in this string is there a colon? That’s going to give me the position and offset of that. So that says that the colon is in position 18. Now, it’s not always going to be 18, sometimes these strings will be a little bit different, okay? So, the next thing I can do is I can say, a small piece of this string is do str and then starting from that position, ipos, through the end of that string. And then we’re going to print that out, print out the piece. And when I’m doing string parsing, tearing strings apart, I tend to have a lot of situations where I print over and over and over again. So now let’s see if that piece is the right piece. And, the answer is, it doesn’t quite look right, because, see, I’ve got that colon there? And, that’s because it says start at 18, position 18, wherever that is, and then keep on going. And, so I need to do ipos plus 1 so let’s see. I will just sort of advance past this little colon character and get into that space, okay? So let’s run it. So now I got’ve space 0 8 4 7 5 and now I can just see if value equals float(piece) because piece is a string. It’s a string, and then I’m going to say print(value) to see if I got the value right. And let’s remember that there’s a space here. This might mess up float. I don’t think it’s going to mess up float, because float’s trying to find a floating point number and it kind of, but let’s just see if it works. Let’s just see if it works. Okay, so the key is there is it’s in position. The colon is in position 18, the string we pulled out is blank 0 8 4 7 5, and the floating point number is 0.8475, so we’ve sort of solved this. Now, I can clean this up a little bit by making that plus 2, so I’ll just change that to plus 2, and you’ll see how that changes what I’m doing. And so now this here is the string, that one there is the string, this is the actual floating point number, they’re the same thing, other than the fact that it’s a floating point number, and you can add something to it. So I could do something like print value plus 42.0, and that would actually work, right? So 42 point, and if did print piece plus 42.0 that will blow up, right? Because piece is a string and 42 is a float and it says can’t convert float object to string implicitly. Okay? And so other than sort of taking out this extra stuff. I’m just commenting out a whole bunch of stuff here. Oops. So I take out all those print statements. These five lines are the lines to do this particular assignment where we are tearing apart a string and in the future the source this is just so that we can play with strings but later we’ll be taking this data from over the place. Finally we’re going to start opening some files and then later in the course we’re going to be doing opening data from databases, we will be opening data from the Internet, and do on. There is all kind of sources of data where we get these strings. But for now, we’re in Chapter 6 and we’re only focused on strings. So I hope you found this useful and coming up soon we’ll be opening files.
مشارکت کنندگان در این صفحه
تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.
🖊 شما نیز میتوانید برای مشارکت در ترجمهی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.