how to convert string to number?

Tim Chase python.list at tim.thechases.com
Wed Oct 17 01:17:35 CEST 2007


> I am new to Python and find it very interesting

welcome to the wonderful world of Python :)

> so I decided to try to port a big project from matlab to 
> python. To prove the value of the python, I need to find an 
> python way to do it.

A good exercise for learning Python.

> The input file contains many lines of data starts with a
> label. The data lengths are not the same and the data type is
> mixed with int and float. Some lines start with comment sign #
> need to be removed from the dictionary. The mixed int and
> float really cause me trouble to convert the data efficiently.
> I will try your suggestion


Just check the results on each line as you iterate over
them...something like

 results = {}
 for line in file('in.txt'):
   line = line.strip()
   if line.startswith('#'): continue
   key, values = line.split(None,1)
   results[key] = map(eval, values.split())

-tkc






More information about the Python-list mailing list