From 5db3a658e941398c422ce54214864d9f6f920702 Mon Sep 17 00:00:00 2001 From: XinYuDu-AI <112471882+XinYuDu-AI@users.noreply.github.com> Date: Mon, 30 Nov 2026 20:24:37 +0800 Subject: [PATCH 1/2] Fix the CSV reader example in the collections.namedtuple docs Refactor file handling for employee records to use a context manager. --- Doc/library/collections.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 40f780c7d049d4..1c8adc294279d1 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -949,8 +949,9 @@ by the :mod:`csv` or :mod:`sqlite3` modules:: EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade') import csv - for emp in map(EmployeeRecord._make, csv.reader(open("employees.csv", "rb"))): - print(emp.name, emp.title) + with open("employees.csv", newline="", encoding="utf-8") as f: + for emp in map(EmployeeRecord._make, csv.reader(f)): + print(emp.name, emp.title) import sqlite3 conn = sqlite3.connect('/companydata') From 2b1b5dbc1cbfe709c93abee3bfb3813d7f1ff10b Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 30 Nov 2026 17:59:16 +0000 Subject: [PATCH 2/2] Fix additional quirks --- Doc/library/collections.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 1c8adc294279d1..fec6081a359eb2 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -949,7 +949,7 @@ by the :mod:`csv` or :mod:`sqlite3` modules:: EmployeeRecord = namedtuple('EmployeeRecord', 'name, age, title, department, paygrade') import csv - with open("employees.csv", newline="", encoding="utf-8") as f: + with open("employees.csv", newline="") as f: for emp in map(EmployeeRecord._make, csv.reader(f)): print(emp.name, emp.title) @@ -959,6 +959,7 @@ by the :mod:`csv` or :mod:`sqlite3` modules:: cursor.execute('SELECT name, age, title, department, paygrade FROM employees') for emp in map(EmployeeRecord._make, cursor.fetchall()): print(emp.name, emp.title) + conn.close() In addition to the methods inherited from tuples, named tuples support three additional methods and two attributes. To prevent conflicts with