Skip to content

bpo-45498: fix csv dictReader code example #28999

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from

Conversation

AkechiShiro
Copy link
Contributor

@AkechiShiro AkechiShiro commented Oct 16, 2021

Fixes an example in the documentation.
Replacing print(row) by print(reader) in DictReader for the csv lib.

https://bugs.python.org/issue45498

@bedevere-bot bedevere-bot added the docs Documentation in the Doc dir label Oct 16, 2021
@AkechiShiro AkechiShiro force-pushed the update-csv-code-example branch from 451f4a8 to c987b7f Compare October 16, 2021 21:27
Copy link
Contributor

@smontanaro smontanaro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code looks fine to me. Here's a simple session:

>>> import csv
>>> with open('names.csv', newline='') as csvfile:
...     reader = csv.DictReader(csvfile)
...     for row in reader:
...         print(row['first_name'], row['last_name'])
... 
Eric Idle
John Cleese
>>> print(reader)
<csv.DictReader object at 0x7f06c1619dc0>
>>> print(row)
{'first_name': 'John', 'last_name': 'Cleese'}

@AkechiShiro
Copy link
Contributor Author

Hi @smontanaro, could you explain to me why the row variable is still defined ? It does indeed work, but the row variable should be only defined inside the scope of the for loop no ?

@AkechiShiro AkechiShiro deleted the update-csv-code-example branch October 16, 2021 21:48
@smontanaro
Copy link
Contributor

The for statement doesn't introduce a new scope. (Nor does the with statement except for the as variable, so reader survives as well.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants