Skip to content

Commit 23e1562

Browse files
miss-islingtonXinYuDu-AIStanFromIreland
authored
[3.15] gh-162868: Fix the collections.namedtuple examples (GH-162869) (#162884)
(cherry picked from commit 114f465) Co-authored-by: XinYuDu-AI <112471882+XinYuDu-AI@users.noreply.github.com> Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 8fa0152 commit 23e1562

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
@@ -940,15 +940,17 @@ by the :mod:`csv` or :mod:`sqlite3` modules::
940940
EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade')
941941

942942
import csv
943-
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
944-
print(emp.name, emp.title)
943+
with open("employees.csv", newline="") as f:
944+
for emp in map(EmployeeRecord._make, csv.reader(f)):
945+
print(emp.name, emp.title)
945946

946947
import sqlite3
947948
conn = sqlite3.connect('/companydata')
948949
cursor = conn.cursor()
949950
cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
950951
for emp in map(EmployeeRecord._make, cursor.fetchall()):
951952
print(emp.name, emp.title)
953+
conn.close()
952954

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

0 commit comments

Comments
 (0)