diff --git a/Doc/library/collections.rst b/Doc/library/collections.rst index eaa69d1da8f250..a7e394e0e52c71 100644 --- a/Doc/library/collections.rst +++ b/Doc/library/collections.rst @@ -936,8 +936,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') @@ -945,6 +946,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