I am working on converting parts of a C++ program to Python, but I have some trouble replacing the C function strtod.
The strings I’m working on consists of simple mathmatical-ish equations, such as “KM/1000.0”. The problem is that the both constants and numbers are mixed and I’m therefore unable to use float().
How can a Python function be written to simulate strtod which returns both the converted number and the position of the next character?
I’m not aware of any existing functions that would do that.
However, it’s pretty easy to write one using regular expressions:
A better overall approach might be to build a lexical scanner that would tokenize the expression first, and then work with a sequence of tokens rather than directly with the string (or indeed go the whole hog and build a yacc-style parser).