# Zhi Yuan "Oscar" Lin
# 3 November 2021
# In this lesson, I learned how to import, open, and read files through examples and tasks. Additionally, I learned the
# importance of the !curl command, for it is necessary to import a file. I also learned how to use the .read() to read
# a specific number of characters.
# There were no major problems or questions throughout this lesson.
# [ ] review and run example
# This program imports a file named "poem1.txt" by using the !curl command.
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
# [ ]Run to open the file in memory as poem_file
# The variable "poem_file" is assigned to the return file object from using the open() function to open the file named
# "poem1.txt" in read mode, and text mode by default.
poem_file = open('poem1.txt', 'r')
# [ ] run and review code to test if open worked
# should display name='poem1.txt' and no errors
# This program displays the value assigned to the poem_file variable in the output.
poem_file
# [ ] import cities.txt
# This program imports a file named "cities.txt" by using the !curl command.
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
# [ ] open cities.txt as cities_file
# [ ] test cities.txt was opened
# The variable "cities_file" is assigned to the return file object from using the open() function to open the file named
# "cities.txt" in read mode, and text mode by default.
cities_file = open('cities.txt', "r")
# This program displays the value assigned to the cities_file variable in the output.
cities_file
# [ ] review and run example
# The variable "poem_contents" is assigned to the given value when the file "poem_file" is attached to the
# file method ".read()".
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
# This program displays the value assigned to the poem_contents variable in the output.
poem_contents
# [ ] review and run example
# since .read() loaded the file as a string it can be printed
# This function displays the value assigned to the poem_contents variable in the output.
print(poem_contents)
# [ ] after import and open of cities.txt in task 1
# [ ] read cities_file as cities
# [ ] display the string: cities
# The variable "cities" is assigned to the given value when the file "cities_file" is attached to the file method ".read()".
cities = cities_file.read()
# This program displays the value assigned to the poem_contents variable in the output.
cities
# [ ] print the string: cities
# This function displays the value assigned to the poem_contents variable in the output.
print(cities)
# [ ] review and run example to read poem1.txt 10 characters at a time
# The variable "poem_file" is assigned to the return file object from using the open() function to open the file named
# "poem1.txt" in read mode, and text mode by default.
poem_file = open('poem1.txt', 'r')
# The variable "poem_10char" is assigned to the first 10 characters of the value assigned to the poem_file variable, by
# using the file method ".read()".
poem_10char = poem_file.read(10)
# This function displays the value assigned to the poem_10char variable in the output.
print(poem_10char)
# This program displays the value assigned to the poem_10char variable in the output.
poem_10char
# [ ] review and run example, + 10 more characters
# reads and displays without storing in a variable
# This program displays the new first 10 characters of the value assigned to the poem_file variable, by using the file
# method ".read()".
poem_file.read(10)
# [ ] review and run example, + 10 more characters
# reads and stores in variable poem_parts
# The variable "poem_parts" is assigned to the new first 10 characters of the value assigned to the poem_file variable,
# by using the file method ".read()".
poem_parts = poem_file.read(10)
# This function displays the value assigned to the poem_parts variable.
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
# The variable "poem_parts" is reassigned to the value assigned to the poem_parts variable concatenated to the first 5
# character of the value assigned to the poem_file variable due to the file method ".read()".
poem_parts += poem_file.read(5)
# This function displays the value assigned to the poem_parts variable.
print(poem_parts)
# [ ] digits of pi
# 1. import digits_of_pi.txt
# This program imports a file named "digits_of_pi.txt" by using the !curl command.
!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
# The variable "digits_of_pi_text" is assigned to the return file object from using the open() function to open the file
# named "digits_of_pi.txt" in read mode and text mode by default.
digits_of_pi_text = open('digits_of_pi.txt')
# The variable "pi_digits" is assigned to the new first 4 characters of the value assigned to the digits_of_pi_text
# variable, by using the file method ".read()".
pi_digits = digits_of_pi_text.read(4)
# This function displays the value assigned to the pi_digits variable in the output.
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*
# The variable "pi_digits" is reassigned to the value assigned to the pi_digits variable concatenated to the first 4
# character of the value assigned to the digits_of_pi_text variable due to the file method ".read()".
pi_digits += digits_of_pi_text.read(4)
# This function displays the value assigned to the pi_digits variable in the output.
print(pi_digits)
# [ ] review and run example
# The variable "poem_file" is assigned to the return file object from using the open() function to open the file
# named "poem1.txt" in read mode, and text mode by default.
poem_file = open('poem1.txt', 'r')
# The variable "poem_part" is assigned to the first 15 characters of the value assigned to the poem_file variable, by
# using the file method ".read()". Additionally, the 15 characters will be change into all uppercases due to the poem_file
# variable also being attached to the string method ".upper()".
poem_part = poem_file.read(15).upper()
# This function displays the value assigned to the poem_print variable in the output.
print(poem_part)
# [ ] review and run example
# The variable "poem_part" is assigned to the new first 6 characters of the value assigned to the poem_file variable, by
# using the file method ".read()". Additionally, the 6 characters will be all capitalize due to the poem_file variable also
# being attached to the string method ".title()".
poem_part = poem_file.read(6).title()
# This function displays the value assigned to the poem_print variable in the output.
print(poem_part)
# [ ] review and run example
# The variable "poem_part" is assigned to the new first 6 characters of the value assigned to the poem_file variable, by
# using the file method ".read()".
poem_part = poem_file.read(6)
# This function displays the value assigned to the poem_print variable in the output.
print(poem_part)
# This function displays the given value when determining if the value assigned to the poem_part variable is all letters by
# using the string method ".isalpha()", and the string "isalpha because of '\n'". Additionally, the single quotes are
# displayed in the output because of the escape character "'\".
print(poem_part.isalpha(), "isalpha() because of `\\n`")
# This program displays the value assigned to the poem_part variable in the output.
poem_part
# [ ] review and run example
# The variable "poem_file" is assigned to the return file object from using the open() function to open the file
# named "poem1.txt" in read mode, and text mode by default.
poem_file = open('poem1.txt', 'r')
# The variable "poem_text" is assigned to the given value when the file "poem_file" is attached to the file method ".read()".
poem_text = poem_file.read()
# This fuction displays the 9th to 25th character of the value assigned to the poem_text variable in the output.
print(poem_text[8:26])
# [ ] complete the task
# The variable "cities_file" is assigned to the return file object from using the open() function to open the file named
# "cities.txt" in read mode, and text mode by default.
cities_file = open('cities.txt','r')
# The variable "cities" is assigned to the given value when the file "cities_file" is attached to the file method ".read()".
cities = cities_file.read()
# The variable "initials" is assigned to an empty string.
initials = ""
# By using the for...in loop, each character of the value assigned to the cities variable will be iterated and be determined
# by an IF statement. The IF statement determines if the value assigned to the chara variable is uppercase by using the
# string method ".isupper()". If true, the value assigned to the initial variable will be reassigned to the value of the
# initial variable concatenated to the value of the chara variable. If false, the program moves on to the elif statement
# and determines if the value assigned to the chara variable is equal to the escape character "\n". If true, a newline would
# be created. If false, the program moves on to the else statement and nothing is execute due to the keyword "pass". This
# loop will continue until each character of the value assigned to the cities variable is iterated.
for chara in cities:
if chara.isupper():
initials += chara
elif chara == "\n":
initials += "\n"
else:
pass
# This function displays the value assigned to the initials variable in the output.
print(initials)