diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 40f780c7d049d4..fec6081a359eb2 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="") as f: + for emp in map(EmployeeRecord._make, csv.reader(f)): + print(emp.name, emp.title) import sqlite3 conn = sqlite3.connect('/companydata') @@ -958,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