0

I have got stuck with Python classes . Please help.

I am getting an error stating that global name 'data' not defined.

5 Answers 5

5

You want to do

def read_data(self):
    with open(self.data, 'r') as f
        ...
Sign up to request clarification or add additional context in comments.

Comments

3

You need to reference data variable using the self variable.

Like -

with open(self.data,'r') as f:

1 Comment

Also , I would advice you to accept answers (for all your questions) (by clicking the tick mark on the left side of the answer) , would really help the community.
1

You have to reference with self.data

Comments

1

You need to correct this line :

with open(data, 'r') as f:

To :

with open(self.data, 'r') as f:

Comments

0

In ParseCSV class constructor you set self.data by data parameter, this means that you can access data by self in other methods of the class, change line 6 to below form:

...
    with open(self.data, 'r') as f:
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.