Attempting To Replace Open() With A Pandas Subset, But I Am Given An __exit__ Error?
Solution 1:
Can anyone help me understand what
__exit__
means in this context? I do not understand from other submissions. ... Astext_file
isn't a function, it should be exitable.
When you use with
statement context managers, that object must define these two methods:
__enter__
__exit__
Whatever text_file[["Name"]]
is (a Pandas DataFrame, it seems), it doesn't implement either of these methods. As indicated by the traceback, it doesn't define __enter__
at all, so execution stops right there and raises an exception.
I don't see a need to use a DataFrame as a context manager. A typical use-case is when you want to ensure that something happens at the end of the with
block, namely, closing a file stream. (Like a try
/finally
block--you want to make sure __exit__
gets called unconditionally.) With a Pandas DataFrame, I'm not sure if there is any analogy that would necessitate have those two dunder methods.
Post a Comment for "Attempting To Replace Open() With A Pandas Subset, But I Am Given An __exit__ Error?"