]> granicus.if.org Git - python/commitdiff
Add example for auto-renaming.
authorRaymond Hettinger <python@rcn.com>
Wed, 8 Apr 2009 07:49:03 +0000 (07:49 +0000)
committerRaymond Hettinger <python@rcn.com>
Wed, 8 Apr 2009 07:49:03 +0000 (07:49 +0000)
Doc/whatsnew/3.1.rst

index f4ffd762739885c957877896399f27a99103e80e..4fb2a595b508a093dcaa828df5f88e915bf7570c 100644 (file)
@@ -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`.)