# Noemi Cabrera
# 8 November 2021
# In this lesson, I learned how to import files in Jupyter Notebooks using the curl command,
# which includes !curl, the file link, and then '-o' plus the new name for the file.
# Then, I learned how to open the imported file in memory using the open() method. Finally,
# I learned to read the opened file using the .read() method to load the entire file as string,
# and the .read(n) method to load a specific number of characters of the file as a string.
# I didn't have any difficulties in this lesson.
# [ In this code, the curl statement is used to import a file given a web adress. Then
# using '-o', the file gets renamed to opoem1.txt ] review and run example
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
# [ The file that was dowloaded to this notebook in the code above is now opened using the
# open() method. The code is stored in a variable. ] Run to open the file in memory as poem_file
poem_file = open('poem1.txt', 'r')
# [ In this code, the variable used in the above code to open the file in Python is written
# to test if the file was actually open or not. If the file was opened, then name = (name of file)
# will appear. If the file was not opened, an error will display. ] run and review code to
# test if open worked
# should display name='poem1.txt' and no errors
poem_file
# [ In this code, the curl statement is used to import a file given a web adress. Then,
# using '-o', the file gets renamed to cities.txt ] import cities.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
# [ The file that was dowloaded to this notebook in the code above is now opened using the
# open() method. The code is stored in the cities_file variable. ] open cities.txt as cities_file
# [ To test if file was opened, the variable containing the file is written by itself.]
# test cities.txt was opened
cities_file = open('cities.txt', 'r')
cities_file
# [ In this code, the read() method is used to load the poem_file into memory as a string.
# This is stored in the poem_contents variable.] review and run example
poem_contents = poem_file.read()
# [ In this code, the previous defined variable poem_contents is written to display the
# contents of the poem_file including formatting characters.]
# review and run example
# shows the file as a string with formatting characters such as "\n", output should be non-blank
poem_contents
# [ SInce we used read(), the poem_file is now a string. Therefore, it can be printed using
# the print() function. ] review and run example
# since .read() loaded the file as a string it can be printed
print(poem_contents)
# In this code, the previously imported and opened cities_file is used. The cities_file is
# loaded as a string using the read() method. This is stored in the variable cities and then
# it's displayed including the formatting characters.
# [ ] after import and open of cities.txt in task 1
# [ ] read cities_file as cities
# [ ] display the string: cities
cities = cities_file.read()
cities
# [ Since we used read(), the cities_file is now a string. Therefore, it can be printed using
# the print() function. ] print the string: cities
print(cities)
# [In this code, the read(n) method is used to load a certain amount of characters as a string
# from poem_file. In this case, the first 10 characters are read. This is stored in the poem_10_char
# variable and then printed. ] review and run example to read poem1.txt 10 characters at a time
poem_file = open('poem1.txt', 'r')
poem_10char = poem_file.read(10)
print(poem_10char)
poem_10char
# [ Since the first 10 characters of poem_file have been printed, the next 10 characters are
# printed because read(10) has been used again in this code. This is not stored in a variable. ]
# review and run example, + 10 more characters
# reads and displays without storing in a variable
poem_file.read(10)
# [ In this code, the next 10 characters are printed because read(10) has been used again.
# This is stored in the variable poem_parts and then printed. ] review and run example, + 10 more characters
# reads and stores in variable poem_parts
poem_parts = poem_file.read(10)
print(poem_parts)
# In this code, the poem_parts variable defined above is being added the next 5 characters of
# poem file using addition. The modified poem_parts variable is printed.
# [ ] REPEATEDLY RUN this cell, + 5 more characters each time run are appended using string addition
# [ ] consider why no additional text displays after multiple runs
poem_parts += poem_file.read(5)
print(poem_parts)
# In this code, the curl statement is used to import a file given a web adress. Then
# using '-o', the file gets renamed to digits_of_pi.txt
# [ ] digits of pi
# 1. import digits_of_pi.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/digits_of_pi -o digits_of_pi.txt
# In this code, the digits_of_pi.txt file is opened by using the open() method. This is stored
# in the digits_of_pi_text variable, which is then read its first 4 characters using the read(n)
# method. This is then stored in the pi_digits variable and printed.
# [ ] digits of pi
# 2. open as digits_of_pi_text
# 3. read() 4 char of digits_of_pi_text to pi_digits variable
# 4. print pi_digits
digits_of_pi_text = open('digits_of_pi.txt','r')
pi_digits = digits_of_pi_text.read(4)
print(pi_digits)
# [ In this code, the pi_digits variable is added the rest of the characters in the digits_of_pi_text
# variable four by four each time this cell runs.] digits of pi
# 5. add to pi_digits string with string addition
# a. add next 4 characters from digits_of_pi obtained from read()
# b. run the cell multiple times to get more digits of *pi*
pi_digits += digits_of_pi_text.read(4)
print(pi_digits)
# [ In this code, the poem_file is opened and then it's first 15 characters are read using the
# read(n) method and made uppercase using the string method .upper(). This is then stored in
# the poem_part variable and printed. ] review and run example
poem_file = open('poem1.txt', 'r')
poem_part = poem_file.read(15).upper()
print(poem_part)
# [ In this code, the next 6 characters of poem_file are read using the
# read(n) method and made uppercase if separated by a space using the string method .title(). This is then stored in
# the poem_part variable and printed.] review and run example
poem_part = poem_file.read(6).title()
print(poem_part)
# [ The next 6 characters of poem_file are read using the read(n) method. This is stored in
# the poem_part variable and printed. Then using the isalpha() boolean string test, this code checks
# if the 6 characters in this code are all aphabetical characters. SInce they're not, False displays. ]
# review and run example
poem_part = poem_file.read(6)
print(poem_part)
print(poem_part.isalpha(), "isalpha() because of `\\n`")
poem_part
# [ In this code, the poem_file is opened again to reset the previous uses of read(n) with
# this file. The entire file is read because the read() method is used. This is stored in the
# poem_text variable and only characters 7 through 24 of the file are printed using string slicing. ]
# review and run example
poem_file = open('poem1.txt', 'r')
poem_text = poem_file.read()
print(poem_text[8:26])
# [ In this code, the cities.txt file is opened using open() and then read using the read() method.
# The variable initials is created with an empty string as its value. Then, using a for..in loop,
# I iterate through each chracter in the cities_file. If the character is upper, then this character
# is added to the string of the initials variable. If the character is "\n", it's added to the string
# of the initials variable. If the character is not upper or "\n", then nothing happens. The final
# string of the initials variable is displayed. ] complete the task
cities_file = open('cities.txt', 'r')
cities = cities_file.read()
initials=""
print(cities)
for city in cities:
if city.isupper() == True:
initials += city
elif city == "\n":
initials += city
else:
pass
print(initials)