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
csv.DictReader inconsistency #47686
Comments
I had to use csv module recently and ran into a "problem" with Python 3.0b2+ (unknown, Jul 24 2008, 12:15:52)
[GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import csv
>>> r = csv.DictReader(open('test.csv'))
>>> r.fieldnames
>>> next(r)
{'baz': '13', 'foo': '42', 'bar': '27'}
>>> r.fieldnames
['foo', 'bar', 'baz']
I think it would be much more useful, if DictReader got 'fieldnames' on
calling __init__ method, so this would look like this:
>>> r = csv.DictReader(open('test.csv'))
>>> r.fieldnames
['foo', 'bar', 'baz'] The easy way to do this is to subclass csv.DictReader. |
I think this is the wrong approach. It would be better to have a |
And how this method should look? Well, adding new API after beta2 is a "no-no" as I understand, so this |
That would be a fairly easy change to the DictReader class (see |
I should also point out that I've generally used this technique to f = open("somefile.csv", "rb")
rdr = csv.DictReader(f, fieldnames=csv.reader(f).next()) So it is fairly trivial to set the fieldnames attribute before actually |
Like Raymond, I have issues with the idea of implicitly reading the As far as working around the absence of such a method goes, I personally r = csv.DictReader(open('test.csv'))
first = next(r)
# Do something with r.fieldnames
for row in chain(first, r):
# Do something with each row |
The consensus seems to be that __init__ shouldn't "magically" read the |
I'm ok with that. :) |
Done... |
I know this has been closed, but perhaps the fieldnames attribute could |
Guido> I know this has been closed, but perhaps the fieldnames attribute It's a nice thought. I tried the straightforward implementation in my Skip |
Re-opened for consideration of GvR's suggestion. |
I personally like the idea of making fieldnames a property - while Hopefully Skip can track down that obscure failure and this change can |
I like the idea of fieldnames attribute being a property, so i've |
Nick, Working with Andrii's patch I'm trying to add a couple test cases to >>> r = csv.DictReader(open("foo.csv", "rb"))
>>> r.fieldnames
['f1', 'f2', 'f3']
>>> r.next()
{'f1': '1', 'f2': '2', 'f3': 'abc'}
>>> r = csv.DictReader(open("foo.csv", "rb"))
>>> first = next(r)
>>> first
{'f1': '1', 'f2': '2', 'f3': 'abc'}
>>> import itertools
>>> for x in itertools.chain(first, r):
... print x
...
f1
f2
f3 If I place first in a list it works: >>> r = csv.DictReader(open("foo.csv", "rb"))
>>> first = next(r)
>>> for x in itertools.chain([first], r):
... print x
...
{'f1': '1', 'f2': '2', 'f3': 'abc'} That makes intuitive sense to me. Is that what you intended? S |
I added a comment to Andrii's patch and added simple test cases |
Andrii, If my view of the Python 3.0 development process is correct and |
Oh, so this is how the process looks like... |
Skip's patch looks good to me (as Skip discovered, I left out the |
Making an existing attribute a property is a nice, API-neutral way to |
Committed as revision 65605. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: