[Tutor] difficulty in reading csv file

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Fri Apr 9 19:57:34 EDT 2004



On Fri, 9 Apr 2004, ABDUL JABBAR SIDDIQUE wrote:


> I am new to Python programming. I am trying to import data from a csv
> file. the data composed of some text values and some integer/float
> values. When I import the data as text through csv reader it works ok.
> however, when I convert the values into float it does'nt work. The
> program runs without throwing any error but it does'nt show any values
> in python shell window. I don't know whats the problem. I'll appreciate
> your help.



Hi Jabbar,


Hmmm... I think we'll need to see some example code that we can test.  At
the moment, there's too little information to diagnose the problem.  Are
you using a module to read the CSV file?


There's a specialized CSV module from Object Craft:

    http://www.object-craft.com.au/projects/csv/

but there's also a 'csv' module in the Standard Library now,

    http://www.python.org/doc/lib/module-csv.html


That's why we need more details, since there are several ways of doing CSV
parsing.  Are you using either of these modules, or have you written your
own parser?


Also, how are you converting the values into floats?  The 'float()'
builtin takes a string, and returns a float:

###
>>> s = '3.1415926'
>>> float(s)
3.1415926000000001
###

and may be useful here.  'csv' itself has no idea if it's reading floats
or ints: it should just be returning each row as a column of strings.


The 'csv' module leaves data conversion to us, so I suspect the bug may
have less to do with 'csv' itself, and more with what happens with the
manipulation done to the string values afterwards.


If you have more questions, please feel free to ask them.  Good luck to
you!




More information about the Tutor mailing list