Extracting floats from a string in Python


 
Thread Tools Search this Thread
Top Forums Programming Extracting floats from a string in Python
# 1  
Old 01-11-2015
Extracting floats from a string in Python

I have this script which can calculate the total of numbers given in a string

Code:
total = 0 
for c in '0123456789': 
   total += int(c) 
print total

How should I modify this script to find the total of if the numbers given in the string form have decimal places? That is, how do I need to modify this line:

Code:
for c in '1.32, 5.32, 4.4, 3.78':

to find the total of these given numbers.
# 2  
Old 01-12-2015
Will this work?
Code:
total = 0
for c  in '1.32', '5.32', '4.4', '3.78':
    c = float(c)
    total += c
print total

It might also work without c = float(c)
Code:
    # c = float(c)
    total += float(c)


Last edited by ongoto; 01-12-2015 at 01:37 AM..
This User Gave Thanks to ongoto For This Post:
# 3  
Old 01-12-2015
Thank You!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting substring within string between 2 token within the string

Hello. First best wishes for everybody. here is the input file ("$INPUT1") contents : BASH_FUNC_message_begin_script%%=() { local -a L_ARRAY; BASH_FUNC_message_debug%%=() { local -a L_ARRAY; BASH_FUNC_message_end_script%%=() { local -a L_ARRAY; BASH_FUNC_message_error%%=() { local... (3 Replies)
Discussion started by: jcdole
3 Replies

2. Programming

Python script for extracting data using two files

Hello, I have two files. File 1 is a list of interested IDs Ex1 Ex2 Ex3File 2 is the original file with over 8000 columns and 20 millions rows and is a compressed file .gz Ex1 xx xx xx xx .... Ex2 xx xx xx xx .... Ex2 xx xx xx xx ....Now I need to extract the information for all the IDs of... (4 Replies)
Discussion started by: nans
4 Replies

3. Shell Programming and Scripting

Remove lines between the start string and end string including start and end string Python

Hi, I am trying to remove lines once a string is found till another string is found including the start string and end string. I want to basically grab all the lines starting with color (closing bracket). PS: The line after the closing bracket for color could be anything (currently 'more').... (1 Reply)
Discussion started by: Dabheeruz
1 Replies

4. Shell Programming and Scripting

Calculation with floats

I want to make computations using floats. This cannot be done with csh. ksh seem to have a problem as well. What is good shell for computations without having to resort to bc or awk? How about python? (5 Replies)
Discussion started by: kristinu
5 Replies

5. Shell Programming and Scripting

integers, floats and text

I am using gawk in a dos shell in windows xp and want to read a datafile and reformat it. The datafile consists of columns of integers, floating point numbers and text strings. Each column is a fixed width and each column contains the same data type, eg all integers, all text. I am looking for a... (0 Replies)
Discussion started by: lookingfor help
0 Replies

6. Shell Programming and Scripting

Extracting String

I am trying to extract a hyperlink from a html document using awk. I have managed to output in the format... href="index.html"> where i would like it just to output index.html. Any ideas on how i would do this? Thanks (2 Replies)
Discussion started by: adpe
2 Replies

7. Shell Programming and Scripting

problem with AWK and floats below 0

Hello. I have a problem with AWK and floats below 0 in a script. It may be simplified to this line (please, take into account that my "locale" is Spanish, so the system will read "," as decimal separator): echo -1,25 2,55745 0,33 ,278 | awk '{print $1+1, $2+1, $3+1, $4+1}'... getting: -0,25... (4 Replies)
Discussion started by: JCastro
4 Replies

8. Shell Programming and Scripting

Extracting a string from one file and searching the same string in other files

Hi, Need to extract a string from one file and search the same in other files. Ex: I have file1 of hundred lines with no delimiters not even space. I have 3 more files. I should get 1 to 10 characters say substring from each line of file1 and search that string in rest of the files and get... (1 Reply)
Discussion started by: mohancrr
1 Replies

9. Shell Programming and Scripting

Multiplying Floats/Decimals

Is there a way that i can get something like this to work: Number=`expr 80 \* 10.69` i.e. To multiply an integer by a decimal or a decimal by a decimal etc...? thanks (10 Replies)
Discussion started by: rleebife
10 Replies

10. Shell Programming and Scripting

extracting from a string

How do I extract 5th to 10th characters of string as given below stored in a shell variable. "ab cd ef gh ij kl" How is cut to be used on this? Thanks for any help. (1 Reply)
Discussion started by: preetikate
1 Replies
Login or Register to Ask a Question