473,371 Members | 1,696 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,371 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 32143
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?...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: 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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.