diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index 55017249999b95..8fe6e7a61f1cc5 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -940,8 +940,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') @@ -949,6 +950,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