[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-06 Thread Greg Ewing
On 7/09/21 5:46 am, C. Titus Brown via Python-ideas wrote: Maybe Greg missed that DictReader.open didn’t exist and was in fact what I was asking for! Sorry about that, I thought you were asking for context manager methods to be added to an existing file-like object. If csv.DictReader had a cl

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-06 Thread Brendan Barnwell
On 2021-09-06 10:50, Christopher Barker wrote: I think the point here is not the context manager, but rather, having the reader open the file itself, rather than taking an already open file-like object. I agree, and I think having such capability is very useful. I'm always annoyed by things

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-06 Thread Thomas Grainger
I really like json.loadf I'd also like to see a csv.loadf. not sure the `f` is needed: you could use @functools.singledispatch On Mon, 6 Sep 2021, 01:12 Christopher Barker, wrote: > On Sun, Sep 5, 2021 at 10:32 AM David Mertz, Ph.D. > wrote: > >> Most Pandas read methods take either a path-like

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-06 Thread Christopher Barker
I think the point here is not the context manager, but rather, having the reader open the file itself, rather than taking an already open file-like object. And if it’s going to do that, it should provide. Context manager. Personally, while we are at it, I’d like to see a “read all” method, analog

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-06 Thread C. Titus Brown via Python-ideas
Thanks for your comments, everyone! Right, I’m struggling to figure out Greg's example :). Maybe Greg missed that DictReader.open didn’t exist and was in fact what I was asking for! (contextlib.closing is great, thank you for that!) Anyway, just to re-up the original points and add a few - * o

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Oscar Benjamin
On Mon, 6 Sept 2021 at 01:13, Greg Ewing wrote: > > On 6/09/21 3:07 am, C. Titus Brown via Python-ideas wrote: > > with csv.DictReader.open(filename) as r: > > for row in r: > >… > > You can do this now: > > from contextlib import closing > with closing(csv.DictReader.open(filename)) a

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Greg Ewing
On 6/09/21 3:07 am, C. Titus Brown via Python-ideas wrote: with csv.DictReader.open(filename) as r: for row in r: … You can do this now: from contextlib import closing with closing(csv.DictReader.open(filename)) as r: ... IMO this is preferable than going around adding context m

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Christopher Barker
On Sun, Sep 5, 2021 at 10:32 AM David Mertz, Ph.D. wrote: > Most Pandas read methods take either a path-like argument or a file-like > argument, and figure out which it is by introspection when called. > Actually, most of them even accept a URL-like argument as well > > I don't think this is a te

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread David Mertz, Ph.D.
Most Pandas read methods take either a path-like argument or a file-like argument, and figure out which it is by introspection when called. Actually, most of them even accept a URL-like argument as well I don't think this is a terrible approach. It doesn't make things quite as explicit as the stan

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Christopher Barker
> This would only be helpful when the CSV is on the disk but csv.reader() > takes a file object so that it can used with anything like a socket for > example. json.load() does the same thing. > There has been discussion about adding loading from a “path like” to the JSON lib. See this list about t

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Rémi Lapeyre
> On 5 Sep 2021, at 17:07, C. Titus Brown via Python-ideas > wrote: > > Hi all, > > the product of Sunday morning idle curiosity... > > I’ve been using the csv module a lot, and I’m wondering if there would be > value in adding a standard mechanism for opening a CSV file (correctly) using

[Python-ideas] Re: Context manager for csv module 'reader' and 'DictReader'?

2021-09-05 Thread Thomas Grainger
Seems nice, tarfile has a similar shortcut too. I do tend to reach for pandas now whenever I can for csv processing On Sun, 5 Sep 2021, 16:10 C. Titus Brown via Python-ideas, < python-ideas@python.org> wrote: > Hi all, > > the product of Sunday morning idle curiosity... > > I’ve been using the cs