Skip to content

bpo-45498: fix csv dictReader code example#28999

Closed
AkechiShiro wants to merge 1 commit into
python:mainfrom
AkechiShiro:update-csv-code-example
Closed

bpo-45498: fix csv dictReader code example#28999
AkechiShiro wants to merge 1 commit into
python:mainfrom
AkechiShiro:update-csv-code-example

Conversation

@AkechiShiro

@AkechiShiro AkechiShiro commented Oct 16, 2021

Copy link
Copy Markdown
Contributor

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

@smontanaro smontanaro left a comment

Copy link
Copy Markdown
Contributor

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
Copy Markdown
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
Copy Markdown
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