0% found this document useful (0 votes)
145 views

Readline While Loop - File .Readline & .Strip Methods - DEV274x Courseware - Edx PDF

Python Tutorial While loop

Uploaded by

Zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views

Readline While Loop - File .Readline & .Strip Methods - DEV274x Courseware - Edx PDF

Python Tutorial While loop

Uploaded by

Zahid
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Microsoft: DEV274x

Introduction to Python: Fundamentals Help zahideme 

Course Discussion Wiki Progress

Course  Module 4 Files  File .readline() & .strip() methods  .readline() while loop

 Previous       Next 

Audit Access Expires Apr. 14, 2019


You lose all access to this course, including your progress, on Apr. 14, 2019.
Upgrade by Mar. 31, 2019 to get:
Unlimited access as long as the course exists
Graded problems and exams
Certi cate of completion
Upgrade now

.readline() while loop


 Bookmark this page

.readline() while loop

To iterate through the lines in a text le,


we can use the .readline() in a while loop.
I've prepared this example by importing
poem1.text
into my Jupyter Notebook and now we open
that le and
assign it to the object poem1.
So now that poem1 is open in memory, we can
do a .readline() on that.
It has the newline ("\n") character at the end.
And we want to remove that again, so we're
going to use a loop to do that.
So I'm just showing you I can go through each
line.
And what I'm gonna do hereis rerun the open,
 0:15 / 2:32  Speed 1.0x  HD     and
th t ill t th i t t th b i i f th

Video Transcripts
Download video le Download SubRip (.srt) le
Download Text (.txt) le

Concept

.readline() in a while loop

poem_line = poem1.readline()
while poem_line:
print(poem_line.capitalize())
poem_line = poem1.readline()

while .readline()

while loop continues while the readline() value in poem_line returns text

a string value evaluates as True in the while loop

an empty string, '', evaluates not True in the while loop

when readline() reaches the end of the le, an empty string is returned

Examples
# [ ] review and run example
# open address to file
poem1 = open('poem1.txt', 'r')

# [ ] review and run example - use a while loop to read each line of a file
# remove last character ('\n') and print as upper case
poem_line = poem1.readline()

while poem_line:
print(poem_line[:-1].upper())
poem_line = poem1.readline()

# [ ] review and run example


poem1.close()

Task 2
while .readline() rainbow colors
assumes rainbow.txt has been imported in task 1

open rainbow.txt as rainbow_ le as read-only

read a color from each line of rainbow_ le in a while loop

print each color capitalized

close rainbow_ le

# [ ] open rainbow.txt as rainbow_text as read-only

# [ ] read the color from lines of rainbow_text in a while loop


# [ ] print each color capitalized as the loop runs

# [ ] close rainbow_text

 Previous Next 

Learn About Veri ed Certi cates


© All Rights Reserved

edX Legal Connect


     
About Terms of Service & Blog
edX for Business Honor Code Contact Us
Privacy Policy Help Center
Accessibility Policy

© 2012–2019 edX Inc.


EdX, Open edX, and MicroMasters are
registered trademarks of edX Inc. | 粤ICP备
17044299号-2

You might also like