From: Raymond Hettinger Date: Wed, 8 Apr 2009 07:49:03 +0000 (+0000) Subject: Add example for auto-renaming. X-Git-Tag: v3.1b1~342 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=4c0b1e4026fc98c264ab18817e74eed94d9f1f42;p=python Add example for auto-renaming. --- diff --git a/Doc/whatsnew/3.1.rst b/Doc/whatsnew/3.1.rst index f4ffd76273..4fb2a595b5 100644 --- a/Doc/whatsnew/3.1.rst +++ b/Doc/whatsnew/3.1.rst @@ -228,7 +228,18 @@ New, Improved, and Deprecated Modules *rename* which lets invalid fieldnames be automatically converted to positional names in the form _0, _1, etc. This is useful when the field names are being created by an external source such as a - CSV header, SQL field list, or user input. + CSV header, SQL field list, or user input:: + + >>> query = input('Enter a query: ') + Enter a query: SELECT region, dept, count(*) FROM main GROUPBY region, dept + + >>> cursor.execute(query) + >>> query_fields = [desc[0] for desc in cursor.description] + >>> UserQuery = namedtuple('UserQuery', query_fields, rename=True) + >>> pprint.pprint([UserQuery(*row) for row in cursor]) + [UserQuery(region='South', dept='Shipping', _2=185), + UserQuery(region='North', dept='Accounting', _2=37), + UserQuery(region='West', dept='Sales', _2=419)] (Contributed by Raymond Hettinger; :issue:`1818`.)