# Noemi Cabrera
# 9 November 2021
# In this lesson, I learned how to use .readline() to read data from file one line at a time
# and how to use .strip() to remove new line characters and other whitespaces. Both methods
# are helpful to use with while and for loops in code. Also, any string method can be applied
# to the lines of the file once the readline() method has been used because they are loaded as strings.
# I didn't have any difficulties in this lesson.
# [In this code, a file is imported into this notebook using the curl statement. The file
# is named poem1.txt ] Run to download file poem1.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
# [ In this code, the previous imported file is opened in read mode using the open() method. This is stored in
# the poem1 variable. ] review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# [ In this code, the readline() method is used to read lines 1,2, and 3 in the poem1 file
# as a string. Each line is displayed with a the '\n' formatting character included at the
# end of each one. ] review and run example
# readline 1, 2, 3
poem_line1 = poem1.readline()
poem_line2 = poem1.readline()
poem_line3 = poem1.readline()
# [ In this code, the 3 variables that contain the 3 first lines of the file poem1 are added
# together and displayed. Since they had '\n' at the end, a newline is displayed in the output
# as well. ] review and run example: print the first 3 .readline() values
print(poem_line1 + poem_line2 + poem_line3)
# [ In this code, the readline() method is used to read the lines in the poem1 file as a
# string. If this cell is run multiple times the next line of the code will displayed
# until there are no more strings. The '\n' formatting character is included at the
# end of each string. ] review and run example printing return value & re-run several times
print(poem1.readline())
# [ In this code, the poem1 file is closed to free resources using the .close() method.]
# review and run example to close file in memory- then run previous cell
poem1.close()
# [ In this code, a file is imported to this notebook using a curl statement. The file is named rainbow.txt. ]
# import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow as rainbow.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow -o rainbow.txt
# [ In this coode, the above imported file is opened in read mode using the open() method.] open rainbow.txt as rainbow_text
rainbow_text = open('rainbow.txt', 'r')
# [ In this code, the first 3 lines of the rainbow.txt file are read and turn into a string using the readline()
# method. Each string is stored into a different variable. ] read the first 3 lines into variables: color1, color2, color3
color1 = rainbow_text.readline()
color2 = rainbow_text.readline()
color3 = rainbow_text.readline()
# [ In this code, the rainbow.txt file is closed using the .close() method. ] close rainbow.txt
rainbow_text.close()
# [ In this code, the first 3 lines of the rainbow.txt file are printed including the formatting they had. ]
# print the first 3 colors
print(color1)
print(color2)
print(color3)
# [ In this code, the poem1.txt file is opened in read mode using the open() method. ]
# review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# [The readline() method is used to read the lines of the poem1 text and this is stored in
# the poem_line variable. Then, the code below the while loop is executed while the readline() value in
# poem_line returns text. Each line is made upper with the .upper() method and displayed.]
# 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()
# [ In this code, the poem1 file is closed using the .close() method. ] review and run example
poem1.close()
# [ In this code, the rainbow.txt file is opened in read mode using the open() method. This
# is stored in the variable rainbow_text.] open rainbow.txt as rainbow_text as read-only
rainbow_text = open('rainbow.txt', 'r')
# [The readline() method is used to read the lines of the rainbow_color text and this is stored in
# the rainbow_color variable. Then, the code below the while loop is executed while the readline()
# value in rainbow_color returns text. Each line is made upper with the .upper() method and then
# displayed.]
# [ ] read the color from lines of rainbow_text in a while loop
# [ ] print each color capitalized as the loop runs
rainbow_color = rainbow_text.readline()
while rainbow_color:
print(rainbow_color[:-1].upper())
rainbow_color = rainbow_text.readline()
# [ In this code, the rainbow_text file is closed using the .close() method. ]
# close rainbow_text
rainbow_text.close()
# [ In this code, the previous imported file is opened in read mode using the open() method. ]
# review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# [ The readline() method is used to read the lines of the poem1 text and this is stored in
# the poem_line variable. Then, the code below the while loop is executed while the readline()
# value in poem_line returns text. Each line is displayed including the '\n' formatting. ]
# 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()
# [ In this code, the poem1 file is re-opened in read mode. The variable poem_line contains
# the lines of the poem1 file with the leading and trailing whitespace characters removed.
# This was done with the .strip() method. Then using a while loop, each string of the file
# is printed. ] 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()
# [ In this code, the rainbow_text file is opened in read mode using the open() method.]
# open rainbow.txt as rainbow_text as read-only
rainbow_text = open("rainbow.txt","r")
# [ The variable rainbow_color contains the lines of the rainbow_text file with the leading
# and trailing whitespace characters removed. This was done with the .strip() method. Then
# using a while loop, each string of the file is made upper and then printed. ]
# 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
rainbow_color = rainbow_text.readline().strip()
while rainbow_color:
print(rainbow_color.upper())
rainbow_color = rainbow_text.readline().strip()
rainbow_text.close()
# [ In this code, a file is imported into this notebook using a curl statement. The file is
# named rainbow_messy.txt ] review and run example: import rainbow_messy.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/rainbow_messy -o rainbow_messy.txt
# [ In this code, the rainbow_messy.txt file is opened in read mode using the open() method.
# This is stored in the rainbow_messy variable.] review and run example: open file read only
rainbow_messy = open('rainbow_messy.txt', 'r')
# [ In this code, the color variable contains the lines of the rainbow_messy file as strings
# since the readline() method is used. Them using a while loop, each string is displayed with
# the formatting characters they had. ] review and run example: .readline() without .strip()
color = rainbow_messy.readline()
while color:
print(color)
color = rainbow_messy.readline()
# [ The rainbow_messy.txt file is re-opened in read mode using open().Then, the rainbow_messy
# variable, which containes the opened file, is used with readline() and strip() to read each
# string and remove the formatting characters '*' and '\n'. Using the while loop, each string is
# printed and then the file is closed.] 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()
# [ In this code, a file is imported into this notebook using a curl statement. The file is named cities_messy.txt ]
# import the file
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities_messy -o cities_messy.txt
# [ The cities_messy.txt file is opened in read mode using open(). This is stored in the cities_messy variable]
# run to read the file into memory
cities_messy = open('cities_messy.txt', 'r')
# [The cities_messy variable, which containes the opened file, is used with readline() and strip()
# to read each string and remove the formatting characters ':', "", and '\n'. Using the while
# loop, each string is printed and then the file is closed. ] 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''")
# [ In this code, a file is imported into this notebook using a curl statement. The file is
# named poem2_messy.txt ] 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
# [ In this code, the poem2_messy.txt is opened in read mode using the open() method. This is stored in the
# poem2_messy variable ] open poem2_messy.txt as poem2_messy in read mode
poem2_messy = open("poem2_messy.txt","r")
# In this code, the poem2_messy file is read line by line and each one loads as a string.
# Then using strip("()\n"), the parentheses and newline format are removed from the strings.
# Using a while loop, each line is then printed without () 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")