Convert String to Int and Arithmetic
James T. Dennis
jadestar at idiom.com
Tue Jun 12 20:56:51 EDT 2007
tereglow <tereglow at yahoo.com> wrote:
> Hello,
> I am a complete newbie to Python and am accustomed to coding in PHP/
> Perl/Shell. I am trying to do the following:
> I have a string:
> cpuSpeed = 'Speed: 1000000000'
> What I would like to do is extract the '1000000000' from the string,
> and divide that by 1000 twice to get the speed of a processor in MHz.
> My understanding is that I need to 'import re' and then use re.split
> to get the first part done. The second part confuses me because I'm
> unable to convert the '1000000000' to an integer to run division
> against it.
> Basically, I want to come out with 1000 for the above string. Any
> help would be appreciated.
> Tom
The most obvious approach, rather hard-coded to the example,
would be:
cpuMhz = float(cpuSpeed[6:]) / 10 ** 6
... take a "slice" of the string, cpuSpeed, from character
seven (zero-based) to the end of the string (cpuSpeed[6:]) and
try to convert it into a floating point number (with the float()
function) and then divide that by the one million
--
Jim Dennis,
Starshine: Signed, Sealed, Delivered
More information about the Python-list
mailing list