# Paul Binder 1-3
# I learned how to read single lines and how to strip unneccessary characters from leading and trailing
# The hardest part was remembering to put ".strip("()\n")" for example in Task 5 on both the top line of code and the last
# downloads poem1 file
# [ ]Run to download file poem1.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
# assigns poem1.txt to poem1
# [ ] review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# assign first 3 lines of the poem to variables
# [ ] review and run example
# readline 1, 2, 3
poem_line1 = poem1.readline()
poem_line2 = poem1.readline()
poem_line3 = poem1.readline()
# prints first 3 lines
# [ ] review and run example: print the first 3 .readline() values
print(poem_line1 + poem_line2 + poem_line3)
# reads the next line of the poem each time rerun
# [ ] review and run example printing return value & re-run several times
print(poem1.readline())
# closes poem file
# [ ] review and run example to close file in memory- then run previous cell
poem1.close()
# import file
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow as rainbow.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow -o rainbow.txt
# open file
# [ ] open rainbow.txt as rainbow_text
rainbow_text = open("rainbow.txt",'r')
# assign 3 lines to variables
# [ ] read the first 3 lines into variables: color1, color2, color3
color1 = rainbow_text.readline()
color2 = rainbow_text.readline()
color3 = rainbow_text.readline()
# close file
# [ ] close rainbow.txt
rainbow_text.close()
# print the 3 variables saved while file was open
# [ ] print the first 3 colors
print(color1+color2+color3)
# open to poem1
# [ ] review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# read each line, uppercased, using while loop
# [ ] 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()
# close the file
# [ ] review and run example
poem1.close()
# open file to rainbow_text
# [ ] open rainbow.txt as rainbow_text as read-only
rainbow_text = open("rainbow.txt","r")
# print each color from the text file
# [ ] read the color from lines of rainbow_text in a while loop
# [ ] print each color capitalized as the loop runs
color_read = rainbow_text.readline()
while color_read:
print(color_read[:-1].upper())
color_read = rainbow_text.readline()
# close file
# [ ] close rainbow_text
rainbow_text.close()
# assign poem1.txt to poem1
# [ ] review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# read each line without removing the new line
# [ ] 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()
# remove extra white space
# [ ] 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()
# open rainbow.txt
# [ ] open rainbow.txt as rainbow_text as read-only
rainbow_text = open('rainbow.txt','r')
rainbow_text
# read each line without extra whitespace and all caps
# [ ] 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
color_line = rainbow_text.readline().strip().upper()
while color_line:
print(color_line)
color_line = rainbow_text.readline().strip().upper()
# import file
# [ ] review and run example: import rainbow_messy.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow_messy -o rainbow_messy.txt
# open file
# [ ] review and run example: open file read only
rainbow_messy = open('rainbow_messy.txt', 'r')
# read lines
# [ ] review and run example: .readline() without .strip()
color = rainbow_messy.readline()
while color:
print(color)
color = rainbow_messy.readline()
# strip asterisk and new lines
# [ ] 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()
# import file
# [ ] import the file
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities_messy -o cities_messy.txt
# open the file
# [ ] run to read the file into memory
cities_messy = open('cities_messy.txt', 'r')
# remove :, \n, and spaces
# [ ] 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")
# import file
# [ ] 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 poem2_messy.txt
# open poem
# [ ] open poem2_messy.txt as poem2_messy in read mode
poem2_messy = open('poem2_messy.txt','r')
# strip () and \n
# [ ] edit while loop to strip the leading and trailing parenthesis, and newlines
# [ ] print the poem
line = poem2_messy.readline().strip("()\n")
while line:
print(line)
line = poem2_messy.readline().strip("()\n")