diff options
author | Raymond Hettinger <python@rcn.com> | 2009-04-08 07:49:03 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-04-08 07:49:03 (GMT) |
commit | 4c0b1e4026fc98c264ab18817e74eed94d9f1f42 (patch) | |
tree | e9b3da0027722526c51187daf9c5928183613944 /Doc/whatsnew | |
parent | 2352cf351917fdc964c4778c55b028dc6523d401 (diff) | |
download | cpython-4c0b1e4026fc98c264ab18817e74eed94d9f1f42.zip cpython-4c0b1e4026fc98c264ab18817e74eed94d9f1f42.tar.gz cpython-4c0b1e4026fc98c264ab18817e74eed94d9f1f42.tar.bz2 |
Add example for auto-renaming.
Diffstat (limited to 'Doc/whatsnew')
-rw-r--r-- | Doc/whatsnew/3.1.rst | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/Doc/whatsnew/3.1.rst b/Doc/whatsnew/3.1.rst index f4ffd76..4fb2a59 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`.) |