473,387 Members | 1,791 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 32146
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: spiros | last post by:
Hi, suppose you have the class Class1 and the main program: class Class1 { public: Class1(); ~Class1();
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
26
by: sam | last post by:
Hi, Can anyone help me find a software that can convert a code in 'C' to 'Fortran77/90' automatically? Thanks in advance. Sam.
19
by: Paul | last post by:
hi, there, for example, char *mystr="##this is##a examp#le"; I want to replace all the "##" in mystr with "****". How can I do this? I checked all the string functions in C, but did not...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
4
by: thomasc1020 | last post by:
This is regarding VB.NET 2003. Variable 'Date' is a string and it contains date information in this format: "DEC/05/2007". Now I am trying to convert the format as "2007-12-05". Is it...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
2
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.