#Helina Hailu
#January 4, 2021
#I learned to using open() and .read() and .read() chosen characters
#I had no difficulty completing this activity.
# [ ] review and run example
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
# [ ]Run to open the file in memory as poem_file
poem_file = open('poem1.txt', 'r')
# [ ] run and review code to test if open worked
# should display name='poem1.txt' and no errors
poem_file
# [ ] import cities.txt
#Using !curl to import the file in the given website address. Then, using -o, the file gets renamed to cities.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
# [ ] open cities.txt as cities_file
# [ ] test cities.txt was opened
cities_file = open('cities.txt', 'r')
cities_file
# [ ] import cities.txt
#!curl is used to import a file given to the web address given. Then, using '-o', the file gets renamed to cities.txt
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
# [ ] open cities.txt as cities_file
# [ ] test cities.txt was opened
cities_file = open('cities.txt', 'r')
cities_file
# [ ] review and run example
poem_contents = poem_file.read()
# [ ] review and run example
# shows the file as a string with formatting characters such as "\n", output should be non-blank
poem_contents
# [ ] review and run example
# since .read() loaded the file as a string it can be printed
print(poem_contents)
# [ ] after import and open of cities.txt in task 1
# [ ] read cities_file as cities
# [ ] display the string: cities
cities = cities_file.read()
cities
# [ ] print the string: cities
print(cities)
# [ ] 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
# [ ] review and run example, + 10 more characters
# reads and displays without storing in a variable
poem_file.read(10)
# [ ] review and run example, + 10 more characters
# reads and stores in variable poem_parts
poem_parts = poem_file.read(10)
print(poem_parts)
# [ ] 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)
# [ ] 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
# [ ] 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)
# [ ] 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)
# [ ] review and run example
poem_file = open('poem1.txt', 'r')
poem_part = poem_file.read(15).upper()
print(poem_part)
# [ ] review and run example
poem_part = poem_file.read(6).title()
print(poem_part)
# [ ] review and run example
poem_part = poem_file.read(6)
print(poem_part)
print(poem_part.isalpha(), "isalpha() because of `\\n`")
poem_part
# [ ] review and run example
poem_file = open('poem1.txt', 'r')
poem_text = poem_file.read()
print(poem_text[8:26])
# [ ] complete the task
#The variables are used to open and read cities.txt. Then the cities are printed. For in is used create initials for the cities
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)