CSV module and "fileobj"

J

Jon Clements

Hi Group,

If I have a CSV reader that's passed to a function, is it possible for
that function to retrieve a reference to the "fileobj" like object
that was passed to the reader's __init__? For instance, if it's using
an actual file object, then depending on the current row, I'd like to
open the file object again, seek to the current position, then create
a csv.reader on that, exhaust it until a certain point, then return.
ie, leaving the passed iterator intact and ready to carry on as
normal.

(And yes, I know passing in the source object is probably easier --
assume a constraint that I can't...)

Cheers,

Jon.
 
S

skip

Jon> If I have a CSV reader that's passed to a function, is it possible
Jon> for that function to retrieve a reference to the "fileobj" like
Jon> object that was passed to the reader's __init__?

Nope, that isn't exposed from the C type.

Skip
 
B

Bruno Desthuilliers

Jon Clements a écrit :
Hi Group,

If I have a CSV reader that's passed to a function, is it possible for
that function to retrieve a reference to the "fileobj" like object
that was passed to the reader's __init__? For instance, if it's using
an actual file object, then depending on the current row, I'd like to
open the file object again, seek to the current position, then create
a csv.reader on that, exhaust it until a certain point, then return.
ie, leaving the passed iterator intact and ready to carry on as
normal.

(And yes, I know passing in the source object is probably easier --
assume a constraint that I can't...)

As Skip pointed out, this won't work that way. There might be a possible
workaround using itertools.tee and eventually StringIO.

My 2 cents
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top