Skip to content

Commit 114f465

Browse files
gh-162868: Fix the collections.namedtuple examples (#162869)
Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 9d6fa5d commit 114f465

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

‎Doc/library/collections.rst‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,15 +949,17 @@ by the :mod:`csv` or :mod:`sqlite3` modules::
949949
EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
950950

951951
import csv
952-
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
953-
print(emp.name, emp.title)
952+
with open("employees.csv", newline="") as f:
953+
for emp in map(EmployeeRecord._make, csv.reader(f)):
954+
print(emp.name, emp.title)
954955

955956
import sqlite3
956957
conn = sqlite3.connect('/companydata')
957958
cursor = conn.cursor()
958959
cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
959960
for emp in map(EmployeeRecord._make, cursor.fetchall()):
960961
print(emp.name, emp.title)
962+
conn.close()
961963

962964
In addition to the methods inherited from tuples, named tuples support
963965
three additional methods and two attributes. To prevent conflicts with

0 commit comments

Comments
 (0)