469,270 Members | 1,720 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,270 developers. It's quick & easy.

Convert String to Int and Arithmetic

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

Jun 12 '07 #1
7 31971
On Jun 12, 9:32 am, tereglow <tereg...@yahoo.comwrote:
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
You don't need to use the "re" module. I'd just do something like
this:
>>temp = cpuSpeed.split(' ')
temp
['Speed:', '1000000000']
# grab the 2nd element from the list and cast it as an integer
>>cpuSpeed = int(temp [1])
cpuSpeed = (cpuSpeed / 1000) / 1000
cpuSpeed
1000

Basically, you just use the split command and cast it as an integer
using the reserved word "int".

Then you can divide it twice.

Mike

Jun 12 '07 #2
On 12 Jun., 16:32, tereglow <tereg...@yahoo.comwrote:
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.
It shall suffice to use string methods in this case:
>>int(cpuSpeed.split(":")[1].strip())
1000000000

Finally you might trim the numerical value by division just as you
wish.
Jun 12 '07 #3
On Jun 12, 10:46 am, Kay Schluehr <kay.schlu...@gmx.netwrote:
On 12 Jun., 16:32, tereglow <tereg...@yahoo.comwrote:
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.

It shall suffice to use string methods in this case:
>int(cpuSpeed.split(":")[1].strip())

1000000000

Finally you might trim the numerical value by division just as you
wish.
Thanks guys. The biggest problem I'm having with Python is that it
makes things too easy to do!

Jun 12 '07 #4
On 6/12/07, tereglow <te******@yahoo.comwrote:
Basically, I want to come out with 1000 for the above string. Any
help would be appreciated.
Tom
There are any number of techniques you can use to parse out the
integer part of the string -- the most generic is to use the re module
to match regular expressions, but you may not have to use that module
if you know the exact form of the strings you are parsing. Once you
parse out the number into a string such as '1000000' you can just cast
it as an integer using some code like
x = int('1000000')
--
Evan Klitzke <ev**@yelp.com>
Jun 12 '07 #5
tereglow <te******@yahoo.comwrote:
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

Jun 13 '07 #6
Kay Schluehr:
>int(cpuSpeed.split(":")[1].strip())
Probably this suffices:

int(cpuSpeed.split(":")[1])

Bye,
bearophile

Jun 13 '07 #7
tereglow wrote:

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.
>>cpuSpeed = 'Speed: 1000000000'
p = cpuSpeed.split(":")
p
['Speed', ' 1000000000']
>>p[1]
' 1000000000'
>>v = int(p[1])
v
1000000000
>>v / 1000000
1000
>>>
--
.. Facundo
..
Blog: http://www.taniquetil.com.ar/plog/
PyAr: http://www.python.org/ar/
Jun 14 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Simon Schaap | last post: by
26 posts views Thread by sam | last post: by
4 posts views Thread by thomasc1020 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.