tags:

views:

643

answers:

3

I am pretty new to python. I need to create a class that loads csv data into a dictionary.

I want to be able to control the keys and value So let say the following code, I can pull out worker1.name or worker1.age anytime i want.

class ageName(object):
'''class to represent a person'''
def __init__(self, name, age):
self.name = name
self.age = age

worker1 = ageName('jon', 40)
worker2 = ageName('lise', 22)

#Now if we print this you see that it`s stored in a dictionary
print worker1.__dict__
print worker2.__dict__
#
'''
{'age': 40, 'name': 'jon'}
#
{'age': 22, 'name': 'lise'}
#
'''
#

#when we call (key)worker1.name we are getting the (value)
print worker1.name
#
'''
#
jon
#
'''

But I am stuck at loading my csv data into keys and value.

[1] I want to create my own keys worker1 = ageName([name],[age],[id],[gender])

[2] each [name],[age],[id] and [gender] comes from specific a column in a csv data file

I really do not know how to work on this. I tried many methods but I failed. I need some helps to get started on this.

---- Edit This is my original code

import csv

# let us first make student an object

class Student():
    def __init__(self):
        self.fname = []
        self.lname = []
        self.ID = []
        self.sport = []
        # let us read this file
        for row in list(csv.reader(open("copy-john.csv", "rb")))[1:]:
            self.fname.append(row[0])
            self.lname.append(row[1])   
            self.ID.append(row[2])
            self.sport.append(row[3])
    def Tableformat(self):
        print "%-14s|%-10s|%-5s|%-11s" %('First Name','Last Name','ID','Favorite Sport')
        print "-" * 45
        for (i, fname) in enumerate(self.fname):
           print "%-14s|%-10s|%-5s|%3s" %(fname,self.lname[i],self.ID[i],self.sport[i])
    def Table(self):
        print self.lname

class Database(Student):
    def __init__(self):
        g = 0
        choice = ['Basketball','Football','Other','Baseball','Handball','Soccer','Volleyball','I do not like sport']
        data = student.sport
        k = len(student.fname)
        print k
        freq = {}
        for i in data:
            freq[i] = freq.get(i, 0) + 1
        for i in choice:
            if i not in freq:
                freq[i] = 0
            print i, freq[i]


student = Student()
database = Database()

This is my current code (incomplete)

import csv
class Student(object):
    '''class to represent a person'''
    def __init__(self, lname, fname, ID, sport):
        self.lname = lname
        self.fname = fname
        self.ID = ID
        self.sport = sport
reader = csv.reader(open('copy-john.csv'), delimiter=',', quotechar='"')
student = [Student(row[0], row[1], row[2], row[3]) for row in reader][1::]
print "%-14s|%-10s|%-5s|%-11s" %('First Name','Last Name','ID','Favorite Sport')
print "-" * 45
for i in range(len(student)):
    print "%-14s|%-10s|%-5s|%3s" %(student[i].lname,student[i].fname,student[i].ID,student[i].sport)

choice = ['Basketball','Football','Other','Baseball','Handball','Soccer','Volleyball','I do not like sport']
lst = []
h = 0
k = len(student)
# 23
for i in range(len(student)):
    lst.append(student[i].sport) # merge together

for a in set(lst):
    print a, lst.count(a)

for i in set(choice):
    if i not in set(lst):
        lst.append(i)
        lst.count(i) = 0
        print lst.count(i)
+1  A: 

Have you looked at the csv module?

import csv
Mark Byers
Yes I did. In fact I have a poorly-written version, but I realize it was a pain in the neck so I decided to do dictionary right away.
JohnWong
+5  A: 
import csv

reader = csv.reader(open('workers.csv', newline=''), delimiter=',', quotechar='"')
workers = [ageName(row[0], row[1]) for row in reader]

workers now has a list of all the workers

>>> workers[0].name
'jon'

added edit after question was altered

Is there any reason you're using old style classes? I'm using new style here.

class Student:
    sports = []
    def __init__(self, row):
       self.lname, self.fname, self.ID, self.sport = row
       self.sports.append(self.sport)
    def get(self):
       return (self.lname, self.fname, self.ID, self.sport)

reader = csv.reader(open('copy-john.csv'), delimiter=',', quotechar='"')
print "%-14s|%-10s|%-5s|%-11s" % tuple(reader.next()) # read header line from csv
print "-" * 45
students = list(map(Student, reader)) # read all remaining lines
for student in students:
    print "%-14s|%-10s|%-5s|%3s" % student.get()

# Printing all sports that are specified by students
for s in set(Student.sports): # class attribute
    print s, Student.sports.count(s)

# Printing sports that are not picked 
allsports = ['Basketball','Football','Other','Baseball','Handball','Soccer','Volleyball','I do not like sport']
for s in set(allsports) - set(Student.sports):
    print s, 0

Hope this gives you some ideas of the power of python sequences. ;)

edit 2, shortened as much as possible... just to show off :P

Ladies and gentlemen, 7(.5) lines.

allsports = ['Basketball','Football','Other','Baseball','Handball',
             'Soccer','Volleyball','I do not like sport']
sports = []
reader = csv.reader(open('copy-john.csv'))
for row in reader:
    if reader.line_num: sports.append(s[3])
    print "%-14s|%-10s|%-5s|%-11s" % tuple(s)
for s in allsports: print s, sports.count(s)
Tor Valamo
Wooo this is exactly what I was looking forreader = csv.reader(open('Book.csv'), delimiter=',', quotechar='"')workers = [ageName(row[0], row[1], row[2]) for row in reader]for i in range(len(workers)): print workers[i].lname, workers[i].fname, workers[i].ID
JohnWong
you can't define "print" as a method *and* use it as a statement on the same module.
nosklo
I was thinking about it as I wrote it, and I thought it could maybe work. But I changed it now.
Tor Valamo
wooo this is cool. I am new to python so I don't know what new / old style is. I will have to to learn it over the break. Can you help me out with one more thing? Your code is great but gives two errors:Traceback (most recent call last): File "D:/Python26/test2.py", line 13, in <module> students = [Student(row) for row in reader] File "D:/Python26/test2.py", line 5, in __init__ (self.lname,self.fname,self.ID,self.sport) = rowValueError: too many values to unpack
JohnWong
oh actually if i add [1::] back will give this error insteadTraceback (most recent call last): File "D:/Python26/test2.py", line 13, in <module> students = [Student(row) for row in reader[1::]]TypeError: '_csv.reader' object is unsubscriptable
JohnWong
I am using 2.6.4 btw, if it helps
JohnWong
I updated a couple of lines around where your problem is. Note that it reads the header line and prints it first, so there's no need to slice the reader (which you can't since it's an iterator, which is why you got the error).
Tor Valamo
Thank you Tor. I have a lot to learn. I will finish my program and come back to thank you and ask for more reference (how to become a very good python coder)
JohnWong
This code can still be improved, I just don't know if this is all you want it to do. If it is, then I can cut it by about 10 lines :P
Tor Valamo
It is what I want... 10 lines? ... omg -_- If you may, I would learn it.... hahaha Thanks
JohnWong
There, just for you ;)
Tor Valamo
nice... Tor. I will not use the short version because it is too good. Hahaha. but please, I am very sorry to bother you. The long version has a small problem which I searched around. for s in set(students.sports): # class attributeAttributeError: 'list' object has no attribute 'sports'. That's it. If I can resolve this, I can implement more on my own. Again, I am really happy to have people like you to help me out... I only had two classes in python and my professor expected me to write a database program in two days... thanks
JohnWong
oh, that should be Student.sports
Tor Valamo
Also the last few lines were changed slightly, so make sure to update those too.
Tor Valamo
Thank you. I am very thankful.
JohnWong
A: 

I second Mark's suggestion. In particular, look at DictReader from csv module that allows reading a comma separated (or delimited in general) file as a dictionary.

Look at PyMotW's coverage of csv module for a quick reference and examples of usage of DictReader, DictWriter

CruiZen
I don't understand why this got a -1 without an explanatory comment. What's wrong with a suggestion to use a DictReader?
CruiZen
@CruiZen: I have to say, the link you posted was quite useful =). Going to vote you up on that. (Mod's, I assuming that's ok?)
victorhooi