open()
, close()
with
to manage connections. .csv
Note that two files are read in on this notebook. These files need to be in the same working directory as your notebook, else Python will not know where the files are on your computer. Please download the accompanying files (news-story.txt
and student_data.csv
) from the class website.
open()
¶Now, let's open this file in Python.
The built-in open()
function opens files on our system. The function takes the following arguments:
file = open("news-story.txt",mode='rt',encoding='UTF-8')
open()
returns a special item type _io.TextIOWrapper
. Note that a file-like-object is loosely defined in Python. Again, we see duck-typing in action: if it looks like a file and behave like a file then, heck, it's probably a file.
type(file)
print(file.read())
print(file.read()) # Once we've read through the items, the file object is empty
file.close()
Opening and forgetting to close files can lead to a bunch of issues --- mainly the mismanagement of computational resources on your machine.
Moreover, close()
is necessary for actually writing files to our computer
Method | Description |
---|---|
._CHUNK_SIZE() |
int([x]) -> integer int(x, base=10) -> integer |
._finalizing() |
bool(x) -> bool |
.buffer() |
Create a new buffered reader using the given readable raw IO object. |
.closed() |
bool(x) -> bool |
.encoding() |
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
.errors() |
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
.line_buffering() |
bool(x) -> bool |
.mode() |
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
.name() |
str(object='') -> str str(bytes_or_buffer[, encoding[, errors]]) -> str |
.readlines() |
Return a list of lines from the stream. |
.reconfigure() |
Reconfigure the text stream with new parameters. |
.write_through() |
bool(x) -> bool |
file = open("news-story.txt",mode='rt',encoding='UTF-8')
file.readlines() # convert all items to a list
# Is the file closed?
file.closed
mode
s¶Mode | Description |
---|---|
r | "open for reading" default |
w | open for writing |
x | open for exclusive creation, failing if the file already exists |
a | open for writing, appending to the end of the file if it exists |
b | binary mode |
t | text mode (default) |
Examples,
mode = 'rb'
→ "read binary"mode = 'wt'
→ "write text"f = open('news-story.txt',mode="rt",encoding='utf-8')
# Print the mode
print(f.mode)
f.close()
f = open('text_file.txt',mode="wt",encoding='utf-8')
f.write('This is an example\n')
f.write('Of writing a file...\n')
f.write('Neat!\n')
f.close()
NOTE that you must
close()
for your lines to be written to the file
Now, read the file back in in "read mode"
f = open('text_file.txt',mode="rt",encoding='utf-8')
print(f.read())
We can even batch write using a container.
sent = "This is a sentence.".split()
print(sent)
# Note here I'm opening the file in "append mode"
f = open('text_file.txt',mode="at",encoding='utf-8')
f.writelines(sent)
f.close()
f = open('text_file.txt',mode="rt",encoding='utf-8')
print(f.read())
Note that \n
is the delimiter for line breaks.
sent2 = []
for word in sent:
new_word = word + "\n"
sent2.append(new_word)
print(sent2)
# Open the file, and write our new sentence list object
f = open('text_file.txt',mode="at",encoding='utf-8')
f.writelines(sent2)
f.close()
f = open('text_file.txt',mode="rt",encoding='utf-8')
print(f.read())
We'll note when looking at the object's attributes that there is an __iter__()
and __next__()
method, meaning we can iterate over the open file object.
file = open("news-story.txt",mode='rt',encoding='UTF-8')
for line in file:
print(line)
file.close()
file = open("news-story.txt",mode='rt',encoding='UTF-8')
for line in file:
if line == '\n':
continue
print(line)
file.close()
# Example: How many words are in each line?
file = open("news-story.txt",mode='rt',encoding='UTF-8')
for line in file:
if line == '\n':
continue
n_words_per_line = len(line.split())
print(n_words_per_line)
file.close()
with
: beyond opening and closing with context managers¶As you'll note, the need to open()
and close()
files can get a bit redundant after awhile. This issue of closing after opening to deal with resource cleanup is common enough that python has a special protocol for it: the with
code block.
with open("news-story.txt",mode='rt',encoding='UTF-8') as file:
for line in file:
if line == '\n':
continue
n_words_per_line = len(line.split())
print(n_words_per_line)
file.closed
See the python documentation for more on the csv
module located in the standard library.
import csv
Reading in .csv data
with open("student_data.csv",mode='rt') as file:
data = csv.reader(file)
for row in data:
print(row)
Writing csv data
# Student data as a nested list.
student_data = [["Student","Grade"],
["Susan","A"],
["Sean","B-"],
["Cody","A-"],
["Karen",'B+']]
# Write the rows with the .writerows() method
with open("student_data.csv",mode='w') as file:
csv_file = csv.writer(file)
csv_file.writerows(student_data)
Assigning value to variables by using DictReader()
/DictWriter()
method. Here our variable names operate as keys that we can easily reference.
with open("student_data.csv", 'r') as file:
csv_file = csv.DictReader(file)
for row in csv_file:
print(row)
with open("student_data.csv", 'r') as file:
csv_file = csv.DictReader(file)
for row in csv_file:
print(f"{row['Student']} received a {row['Grade']} in the course")
Writing csv file types as dictionaries
with open("student_data.csv", 'w') as file:
variable_names = ["Student","Grade"]
csv_file = csv.DictWriter(file, fieldnames=variable_names)
csv_file.writeheader()
for student in student_data[1:]:
csv_file.writerow({'Student':student[0],'Grade':student[1]})
In a csv, commas are used to separate values, but we could just as easily use something else to separate values.
with open("student_data.csv", 'r') as file:
csv_file = csv.reader(file, delimiter = ",") # comma separated values
with open("only_student_data.csv", 'w') as new_file:
new_csv_file = csv.writer(new_file, delimiter = "\t") # tab separated values
for row in csv_file:
new_csv_file.writerow(row) # only write the student's name