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 Remove characters using .strip()
4.3 File Read a line at a time with .readline()
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 text from files 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
Examples
# [ ] Run to download file to notebook
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem1.txt -o poem1.txt
# [ ] review and run example
# open address to file
poem1 = open('poem1.txt', 'r')
# readlines and print as a list
poem_lines = poem1.readlines()
poem_lines
# [ ] review and run example
for line in poem_lines:
print(line)
Task 1
.readlines()
¶.readlines()
# [ ] import cities
#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
# [ ] open cities.txt as cities_file and read the file as a list: cities_lines
#Answer:
cities_file = open('cities.txt','r')
cities_lines = cities_file.readlines()
# [ ] use list iteration to print each city in cities_lines list
#Answer:
for line in cities_lines:
print(line)
Concepts
for line in poem_lines:
poem_lines[count] = line[:-1]
count += 1
line[:-1]
sets the end point at the last character of the string, the result is the '\n'
(newline) character is omitted
list item | list item contents |
---|---|
poem_lines[0] | 'Loops I repeat\n' |
poem_lines[1] | 'loops\n' |
poem_lines[2] | 'loops\n' |
poem_lines[3] | 'I repeat\n' |
... | ... |
Examples
This example assumes that poem1.txt has been imported in 1st example above
# [ ] review and run examples
# [ ] re-open file and read file as a list of strings
poem1 = open('poem1.txt', 'r')
poem_lines = poem1.readlines()
print(poem_lines)
# [ ] print each list item
for line in poem_lines:
print(line)
# [ ] remove the last character of each list item, which is "\n"
count = 0
for line in poem_lines:
poem_lines[count] = line[:-1]
count += 1
print(poem_lines)
# [ ] print each list item
for line in poem_lines:
print(line)
# [ ] re-open file and read file as a list of strings
# [ ] open cities.txt as cities_file and read the file as a list: cities_lines
#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
cities_file = open('cities.txt','r')
cities_lines = cities_file.readlines()
# [ ] remove the last character, "\n", of each cities_lines list item
count = 0
for line in cities_lines:
cities_lines[count] = line[:-1]
count += 1
print(cities_lines)
# [ ] print each list item in cities_lines
for line in cities_lines:
print(line)
Examples
This example assumes that poem1.txt has been imported in 1st example above
# [ ] review and run example: open and readlines of poem1.txt
poem1 = open('poem1.txt', 'r')
# [ ] review and run example: readlines breaks if file is no longer open
poem_lines = poem1.readlines()
print(poem_lines)
# [ ] review and run example: Close poem1
poem1.close()
# [ ] open cities.txt as cities_file
#Answer:
#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/cities -o cities.txt
cities_file = open('cities.txt','r')
# [ ] read the lines as cities_lines
#Answer:
cities_lines = cities_file.readlines()
# [ ] print the cities that start with the letter "D" or greater
#Answer:
print (cities_lines)
for line in cities_lines:
if(line[0].upper() >= 'D'):
print(line)
# [ ] test that file is closed
#Answer:
cities_file.close()
# [ ] close cities_file
#Answer:
print("Is CLose: ",cities_file.closed)
Task 4
write each item in its own cell
# [ ] import https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem2.txt as poem2.txt
#Answer:
!curl https://raw.githubusercontent.com/MicrosoftLearning/intropython/master/poem2.txt -o poem2.txt
# [ ] open poem2.txt as poem2_text in read mode
#Answer:
poem2_file = open('poem2.txt','r')
# [ ] create a list of strings, called poem2_lines, from each line of poem2_text
#Answer:
poem2_lines = poem2_file.readlines()
# [ ] remove the newline character for each list item in poem2_lines
#Answer:
count = 0
for line in poem2_lines:
poem2_lines[count] = line[:-1]
count += 1
print(poem2_lines)
# [ ] print the poem2 lines in reverse order
#Answer:
poem2_lines.reverse()
print(poem2_lines)
Terms of use Privacy & cookies © 2017 Microsoft