#COnner Kahn
#11/11
#I learned about stripping lines
# [ ]Run to download file poem1.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 56 100 56 0 0 615 0 --:--:-- --:--:-- --:--:-- 615
# [ ] review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# [ ] review and run example
# readline 1, 2, 3
poem_line1 = poem1.readline()
poem_line2 = poem1.readline()
poem_line3 = poem1.readline()
# [ ] review and run example: print the first 3 .readline() values
print(poem_line1 + poem_line2 + poem_line3)
Loops I repeat
loops
loops
# [ ] review and run example printing return value & re-run several times
print(poem1.readline())
break
# [ ] review and run example to close file in memory- then run previous cell
poem1.close()
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow as rainbow.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow -o rainbow.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 43 100 43 0 0 488 0 --:--:-- --:--:-- --:--:-- 488
# [ ] open rainbow.txt as rainbow_text
rainbowtext = open('rainbow.txt', 'r')
# [ ] read the first 3 lines into variables: color1, color2, color3
color1 = rainbowtext.readline()
color2 = rainbowtext.readline()
color3 = rainbowtext.readline()
# [ ] close rainbow.txt
rainbowtext.close
# [ ] print the first 3 colors
print(color1,color2,color3)
red
orange
yellow
# [ ] 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()
LOOPS I REPEAT
LOOPS
LOOPS
LOOPS
I REPEAT
UNTIL I
BREAK
# [ ] review and run example
poem1.close()
# [ ] open rainbow.txt as rainbow_text as read-only
rainbow_file = open("rainbow.txt", 'r')
# [ ] read the color from lines of rainbow_text in a while loop
# [ ] print each color capitalized as the loop runs
for line in rainbow_file:
line = line.capitalize()
print(line)
Red
Orange
Yellow
Green
Blue
Indigo
Violet
# [ ] close rainbow_text
rainbow_file.close
# [ ] review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# [ ] review and run example - readline while loop without removing '\n'
poem_line = poem1.readline()
while poem_line:
print(poem_line)
poem_line = poem1.readline()
poem1.close()
Loops I repeat
loops
loops
loops
I repeat
until I
break
# [ ] review and run example - readline with .strip() to remove '\n'
poem1 = open('poem1.txt', 'r')
poem_line = poem1.readline().strip()
while poem_line:
print(poem_line)
poem_line = poem1.readline().strip()
poem1.close()
Loops I repeat
loops
loops
loops
I repeat
until I
break
# [ ] open rainbow.txt as rainbow_text as read-only
rainbow_file = open("rainbow.txt", 'r')
# [ ] read a color from each line of rainbow_text in a while loop
# use .strip to remove the whitespace '\n' character
# print each color upper case
for line in rainbow_file:
line = line.strip().upper()
print(line)
RED
ORANGE
YELLOW
GREEN
BLUE
INDIGO
VIOLET
# [ ] review and run example: import rainbow_messy.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow_messy -o rainbow_messy.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 71 100 71 0 0 645 0 --:--:-- --:--:-- --:--:-- 645
# [ ] review and run example: open file read only
rainbow_messy = open('rainbow_messy.txt', 'r')
# [ ] review and run example: .readline() without .strip()
color = rainbow_messy.readline()
while color:
print(color)
color = rainbow_messy.readline()
**red**
**orange**
**yellow**
**green**
**blue**
**indigo**
**violet**
# [ ] review and run example: strip "*" and newline ('\n')
rainbow_messy = open('rainbow_messy.txt', 'r')
color = rainbow_messy.readline().strip('*\n')
while color:
print(color)
color = rainbow_messy.readline().strip('*\n')
rainbow_messy.close()
red
orange
yellow
green
blue
indigo
violet
# [ ] import the file
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities_messy -o cities_messy.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 77 100 77 0 0 663 0 --:--:-- --:--:-- --:--:-- 658
# [ ] run to read the file into memory
cities_messy = open('cities_messy.txt', 'r')
# [ ] edit the code to remove leading or trailing colon, newline and space characters
line = cities_messy.readline().strip(":\n")
while line:
print(line)
line = cities_messy.readline().strip(":\n")
Beijing
Cairo
London
Nairobi
New York City
Sydney
Tokyo
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem2_messy as poem2_messy.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem2_messy -o poem2messy.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 75 100 75 0 0 937 0 --:--:-- --:--:-- --:--:-- 937
# [ ] open poem2_messy.txt as poem2_messy in read mode
poem2messy = open("poem2messy.txt", 'r')
# [ ] edit while loop to strip the leading and trailing parenthesis, and newlines
# [ ] print the poem
line = poem2messy.readline().strip("(\n").strip(")")
while line:
print(line)
line = poem2messy.readline().strip("(\n").strip(")")
while True
I loop
True
loop
True
loop
not True
False
end