2

First time writing a class here and I need a little help.

I've been trying to write a class in which the first takes a tab-delimited csv file and outputs a list of dictionaries. Each of the keys in the dictionary is a column title in the csv.

So far, this is what my class looks like:

import csv class consolidate(object): def __init__(self, file): self.file = file def create_master_list(self): with(open(self,'rU')) as f: f_d = csv.DictReader(f, delimiter = '\t') m_l = [] for d in f_d: m_l.append(d) return m_l 

When I try to pass it a file, as follows:

c = consolidate() a = c.create_master_list('Abilities.txt') 

I get the following error:

TypeError: __init__() takes exactly 2 arguments (1 given) 

I know that what I want to pass a file argument to the create_master_list function, but I'm unsure what the right syntax to do this is.

I've tried self.file and file as arguments, and both do not work as well.

Thanks!

2
  • Woohh! First class written. Thank you all! Commented Aug 5, 2013 at 20:48
  • Congratulations! You should consider using class names starting with a capital letter, though. Commented Aug 5, 2013 at 20:51

3 Answers 3

4

Problem

You did not supply second argument for __init__():

class consolidate(object): def __init__(self, file): self.file = file # rest of the code 

while you are instantiating it like this:

c = consolidate() 

Solution

This should work. Change class definition to this:

import csv class consolidate(object): def __init__(self, filename): self.filename = filename def create_master_list(self): with open(self.filename, 'rU') as f: f_d = csv.DictReader(f, delimiter='\t') m_l = [] for d in f_d: m_l.append(d) return m_l 

and then use it like this:

c = consolidate('Abilities.txt') a = c.create_master_list() 

This is one way of achieving the fix.

Note: I also changed the naming (self.file suggested it is file object, while it actually is a file name, thus self.filename). Also keep in mind that the path is relative to from where you execute the script.

Sign up to request clarification or add additional context in comments.

Comments

3

You should pass the file as a parameter to __init__.

c = consolidate ('abilities.txt') 

Then inside create_master_list you should open self.file.

with (open (self.file, 'rU') ) as f: 

Now you can call

a = c.create_master_list () 

4 Comments

Since OP seems to be a beginner in Python, why aren't you following PEP8? In general, Python community follows it (unless there is a reason not to, like when library already has some coding style established before creation of PEP8), so why break it when not necessary?
Interesting that I am frequently asked the same question. I am not following PEP8 because I don't like it. I do NOT want to encourage or discourage the use of PEP8 (or any other coding guideline). I personally just use the spacing I most prefer. Suum cuique.
@Hyperboreus: If you get this question frequently, by a wide range of different people, maybe you should consider changing your behavior. You can write your own code any way you want, but code that you write to instruct beginners will be more useful if you write it the same way everyone else does.
Thanks for your advice abarnert. I will take it into consideration, although I think that "everyone" is a rather quick generalization.
2

That's because your __init__ method of consolidate needs an argument for file:

def __init__(self, file): 

but you don't give it anything:

c = consolidate() 

To fix this problem, change your class like so:

import csv # I capitalized the name of this class because that is convention class Consolidate(object): def __init__(self, file): self.file = file def create_master_list(self): # 'self' is the instance of 'Consolidate' # you want to open 'self.file' instead, which is the file with(open(self.file,'rU')) as f: f_d = csv.DictReader(f, delimiter = '\t') m_l = [] for d in f_d: m_l.append(d) return m_l 

and then use it like this:

c = Consolidate('Abilities.txt') a = c.create_master_list() 

3 Comments

Exactly. But note that after this change, there are some further problems. c.create_master_list should not take a file parameter, because you already got it in __init__, and then it needs to use open(self.file…) rather than open(self…).
@abarnert - I know, give me a second. :) I had to type everything and then scan it for any mistakes.
You forgot self in the signature for create_master_list()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.