Skip to content

Commit 932c91f

Browse files
miss-islingtonXinYuDu-AIStanFromIreland
authored
[3.14] gh-162868: Fix the collections.namedtuple examples (GH-162869) (#162885)
(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 ac0487e commit 932c91f

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

938938
import csv
939-
for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))):
940-
print(emp.name, emp.title)
939+
with open("employees.csv", newline="") as f:
940+
for emp in map(EmployeeRecord._make, csv.reader(f)):
941+
print(emp.name, emp.title)
941942

942943
import sqlite3
943944
conn = sqlite3.connect('/companydata')
944945
cursor = conn.cursor()
945946
cursor.execute('SELECT name, age, title, department, paygrade FROM employees')
946947
for emp in map(EmployeeRecord._make, cursor.fetchall()):
947948
print(emp.name, emp.title)
949+
conn.close()
948950

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

0 commit comments

Comments
 (0)