Re: [Tutor] how to read from a csv file?

2007-10-22 Thread Eric Walstad
Tim Golden wrote:
> You really just want to rename __init__ to rawdata
> and drop the redundant "return rawdata" function.

But remember to include the line:
return rawdata

at the end of your function or else nothing will be printed when
print csvdatareader.rawdata()
is called.

Also, I'd consider using different names for the function and list variable.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] how to read from a csv file?

2007-10-22 Thread Tim Golden
pierre cutellic wrote:
> Hi, this is a module i wrote to catch some data from a csv file:
> 
> ##
> #module csv data reader
> # open a csv file and return its data
> 
> import csv
> import sys
> 
> def __init__(self):
> 

Stop right there. You're confusing modules and
classes. A class has a (meaningful) __init__ method,
which is called when the class is instantiated. You
*can* have an __init__ in a module, but it doesn't
do anything special.

You really just want to rename __init__ to rawdata
and drop the redundant "return rawdata" function.

TJG
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] how to read from a csv file?

2007-10-22 Thread pierre cutellic
Hi, this is a module i wrote to catch some data from a csv file:

##
#module csv data reader
# open a csv file and return its data

import csv
import sys

def __init__(self):

rawdata = []
f = open('datalisting_000.csv','r')

try:

reader = csv.reader(f)

for row in reader:

rawdata.append(row)

finally:

f.close()


def rawdata():

return rawdata

if __name__ == "__main__":
print "This is a module. You can't run it directly. Try to import it."
raw_input("\n\nPress any key to exit.")
#

But when i try to use like:

##
import csvdatareader
print csvdatareader.rawdata()
##

It didn't print anything. Where is the mistake?

Cheers.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor