attempting to replace open() with a pandas subset, but I am given an __exit__ error?












3















I am trying to work with pylabels to create nametags for an upcoming event. In one section of the code, there is this tid-bit:



with open(os.path.join(base_path, "names.txt")) as names:
sheet.add_labels(name.strip() for name in names)


where sheet = labels.Sheet(specs, write_name, border=True). So essentially, this will load each line of "names.txt" and call the function 'write_name', using specifications in 'specs', and add each name to unique labels. I'm attempting to change this code to the following:



with text_file[["Name"]] as names:
sheet.add_labels(name.strip() for name in names)


But I get this error:



Traceback (most recent call last):
File "sticker.V.7.py", line 173, in <module>
with text_file[["Name"]] as names:
AttributeError: __exit__


Can anyone help me understand what exit means in this context? I do not understand from other submissions.



I am hoping to add this subsetting aspect so that I can add further details to the nametags.



I am using Python3.5










share|improve this question

























  • What is text_file and why [["Name"]], obviously it is not something exitable and thus cannot be used with with.

    – MatrixTai
    Nov 15 '18 at 4:05













  • text_file is a dataframe made through pd.read_csv. [['Name']] is a column of the text_file dataframe. as text_file isn't a function, it should be exitable

    – agenttiny
    Nov 15 '18 at 4:34
















3















I am trying to work with pylabels to create nametags for an upcoming event. In one section of the code, there is this tid-bit:



with open(os.path.join(base_path, "names.txt")) as names:
sheet.add_labels(name.strip() for name in names)


where sheet = labels.Sheet(specs, write_name, border=True). So essentially, this will load each line of "names.txt" and call the function 'write_name', using specifications in 'specs', and add each name to unique labels. I'm attempting to change this code to the following:



with text_file[["Name"]] as names:
sheet.add_labels(name.strip() for name in names)


But I get this error:



Traceback (most recent call last):
File "sticker.V.7.py", line 173, in <module>
with text_file[["Name"]] as names:
AttributeError: __exit__


Can anyone help me understand what exit means in this context? I do not understand from other submissions.



I am hoping to add this subsetting aspect so that I can add further details to the nametags.



I am using Python3.5










share|improve this question

























  • What is text_file and why [["Name"]], obviously it is not something exitable and thus cannot be used with with.

    – MatrixTai
    Nov 15 '18 at 4:05













  • text_file is a dataframe made through pd.read_csv. [['Name']] is a column of the text_file dataframe. as text_file isn't a function, it should be exitable

    – agenttiny
    Nov 15 '18 at 4:34














3












3








3








I am trying to work with pylabels to create nametags for an upcoming event. In one section of the code, there is this tid-bit:



with open(os.path.join(base_path, "names.txt")) as names:
sheet.add_labels(name.strip() for name in names)


where sheet = labels.Sheet(specs, write_name, border=True). So essentially, this will load each line of "names.txt" and call the function 'write_name', using specifications in 'specs', and add each name to unique labels. I'm attempting to change this code to the following:



with text_file[["Name"]] as names:
sheet.add_labels(name.strip() for name in names)


But I get this error:



Traceback (most recent call last):
File "sticker.V.7.py", line 173, in <module>
with text_file[["Name"]] as names:
AttributeError: __exit__


Can anyone help me understand what exit means in this context? I do not understand from other submissions.



I am hoping to add this subsetting aspect so that I can add further details to the nametags.



I am using Python3.5










share|improve this question
















I am trying to work with pylabels to create nametags for an upcoming event. In one section of the code, there is this tid-bit:



with open(os.path.join(base_path, "names.txt")) as names:
sheet.add_labels(name.strip() for name in names)


where sheet = labels.Sheet(specs, write_name, border=True). So essentially, this will load each line of "names.txt" and call the function 'write_name', using specifications in 'specs', and add each name to unique labels. I'm attempting to change this code to the following:



with text_file[["Name"]] as names:
sheet.add_labels(name.strip() for name in names)


But I get this error:



Traceback (most recent call last):
File "sticker.V.7.py", line 173, in <module>
with text_file[["Name"]] as names:
AttributeError: __exit__


Can anyone help me understand what exit means in this context? I do not understand from other submissions.



I am hoping to add this subsetting aspect so that I can add further details to the nametags.



I am using Python3.5







python python-3.x pandas contextmanager






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 9:20









cricket_007

82.5k1143111




82.5k1143111










asked Nov 15 '18 at 4:01









agenttinyagenttiny

184




184













  • What is text_file and why [["Name"]], obviously it is not something exitable and thus cannot be used with with.

    – MatrixTai
    Nov 15 '18 at 4:05













  • text_file is a dataframe made through pd.read_csv. [['Name']] is a column of the text_file dataframe. as text_file isn't a function, it should be exitable

    – agenttiny
    Nov 15 '18 at 4:34



















  • What is text_file and why [["Name"]], obviously it is not something exitable and thus cannot be used with with.

    – MatrixTai
    Nov 15 '18 at 4:05













  • text_file is a dataframe made through pd.read_csv. [['Name']] is a column of the text_file dataframe. as text_file isn't a function, it should be exitable

    – agenttiny
    Nov 15 '18 at 4:34

















What is text_file and why [["Name"]], obviously it is not something exitable and thus cannot be used with with.

– MatrixTai
Nov 15 '18 at 4:05







What is text_file and why [["Name"]], obviously it is not something exitable and thus cannot be used with with.

– MatrixTai
Nov 15 '18 at 4:05















text_file is a dataframe made through pd.read_csv. [['Name']] is a column of the text_file dataframe. as text_file isn't a function, it should be exitable

– agenttiny
Nov 15 '18 at 4:34





text_file is a dataframe made through pd.read_csv. [['Name']] is a column of the text_file dataframe. as text_file isn't a function, it should be exitable

– agenttiny
Nov 15 '18 at 4:34












1 Answer
1






active

oldest

votes


















2















Can anyone help me understand what __exit__ means in this context? I do not understand from other submissions. ... As text_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.






share|improve this answer
























  • Thanks for the advice! ill try another method that includes an exit call.

    – agenttiny
    Nov 15 '18 at 8:05











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312256%2fattempting-to-replace-open-with-a-pandas-subset-but-i-am-given-an-exit-er%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2















Can anyone help me understand what __exit__ means in this context? I do not understand from other submissions. ... As text_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.






share|improve this answer
























  • Thanks for the advice! ill try another method that includes an exit call.

    – agenttiny
    Nov 15 '18 at 8:05
















2















Can anyone help me understand what __exit__ means in this context? I do not understand from other submissions. ... As text_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.






share|improve this answer
























  • Thanks for the advice! ill try another method that includes an exit call.

    – agenttiny
    Nov 15 '18 at 8:05














2












2








2








Can anyone help me understand what __exit__ means in this context? I do not understand from other submissions. ... As text_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.






share|improve this answer














Can anyone help me understand what __exit__ means in this context? I do not understand from other submissions. ... As text_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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 4:32









Brad SolomonBrad Solomon

13.7k83486




13.7k83486













  • Thanks for the advice! ill try another method that includes an exit call.

    – agenttiny
    Nov 15 '18 at 8:05



















  • Thanks for the advice! ill try another method that includes an exit call.

    – agenttiny
    Nov 15 '18 at 8:05

















Thanks for the advice! ill try another method that includes an exit call.

– agenttiny
Nov 15 '18 at 8:05





Thanks for the advice! ill try another method that includes an exit call.

– agenttiny
Nov 15 '18 at 8:05




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312256%2fattempting-to-replace-open-with-a-pandas-subset-but-i-am-given-an-exit-er%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python