4.1 File import in Jupyter Notebooks
4.1 File open(
) and .read()
4.2 File Read as a list with .readlines()
4.2 File Closing to free resources with .close()
4.3 File Read a line at a time with .readline()
4.3 Remove characters using .strip()
4.4 File .write()
with .seek()
4.4 File append mode
> Student will be able to
4.1 Import files in Jupyter Notebooks using the curl command
4.1 open()
and .read()
local files in memory
4.1 .read()
a specific number of characters
4.2 Use .readlines()
to read data from file as a list of lines
4.2 Use .close
to free system resources
4.3 Use .readline()
to read data from file a line at a time
4.3 Use .strip()
to remove new line characters
4.4 .write()
data to a new local file
4.4 Use .seek()
to set file read or write location
4.4 Use file append mode
Concepts
below is a code using curl to import poem1.txt, the code is in a command line interface syntax
>#### !curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
The table explains each element of the command above
code | meaning |
---|---|
! |
runs command interface supporting curl |
curl |
enables curl that can download files |
https://raw.githubusercontent.com/... |
is the address for data file to import |
-o |
tells curl write data to a file |
poem1.txt |
name curl will give the file |
Examples
# [ ] review and run example
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
Concepts
>python
poem_file = open('poem1.txt', 'r')
'r'
¶MODE | Description |
---|---|
'r' | read only mode |
'w' | write - overwrites file with same name |
'r+' | read and write mode |
'a' | opens for appending to end of file |
open()
creates an object that can be addressed in python code¶Examples
# [ ]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
Task 1
# [ ] import cities.txt
#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
# [ ] open cities.txt as cities_file
# [ ] test cities.txt was opened
#Answer:
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)
Task 2
# [ ] after import and open of cities.txt in task 1
# [ ] read cities_file as cities
# [ ] display the string: cities
#Answer:
cities = cities_file.read()
# [ ] print the string: cities
#Answer:
print (cities)
Concepts
.read(n)
where n = number of characters to read¶poem_file.read(10)
runs, the next 10 characters are read.¶> Note: if .read(10) result is = '' (or empty string with no characters), it is likely that the end of the file has been reached. Perform a fresh .open() to reset read() to the beginning of the file.
# [ ] 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)
Task 3
# [ ] digits of pi
# 1. import digits_of_pi.txt
#Answer:
!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
#Answer:
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*
#Answer:
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])
Task 4
read()
cities_file into a variable called cities # [ ] compelete the task
#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
cities_file = open('cities.txt', 'r')
cities = cities_file.read()
initials = ""
for letter in cities:
if (letter.isupper()):
initials += letter
elif (letter=="\n"):
initials += "\n"
print('initials: ',initials)
Terms of use Privacy & cookies © 2017 Microsoft