I am trying to read in a database containing lines of numbers (in string 
format) and convert each of these numbers to Rational.  
For example 
'1.2 3 4 5/6  7  8.2'
becomes
[6/5, 3, 4, 5/6, 7, 41/5]

I wrote the below small utility function that takes in a string s and 
converts it to rational if possible.  The problem is that I feel it's too 
slow when having to apply to a large database.  Is there a way to speed up 
the process ?  



def str2rat(s):
    try:
        return Rational(s)
    except TypeError:
        pass

    try:
        return Rational(RealNumber(s))
    except TypeError:
        print 'W: cannot convert %s to rational' %s
        return None


Thanks

-- 
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org

Reply via email to