Selection of different numbers

Hii all ,
I have a file like that
00:00:08
ss ssone 32.0
ssone sstwo 33.0
sstwo ssthree 35.0
11:12:13
ssfour ssfive 34.0
ssthree sssix 38.0
ssnine ssseven 39.0
13:12:11

I want to get all the numbers explicitely.

Desired output will be
00
00
08
32.0
33.0
35.0
11
12
13

and so on.

Could you please tell me how do i can write the code ?

Thanks

This may do what you need:

file = '''00:00:08
ss ssone 32.0
ssone sstwo 33.0
sstwo ssthree 35.0
11:12:13
ssfour ssfive 34.0
ssthree sssix 38.0
ssnine ssseven 39.0
13:12:11
'''

file_list = file.split()

output = []

for item in file_list:
    if item[0].isdigit():
        if item.find(':') > 0:
            output += [n for n in item.split(':')]
        else:
            output.append(item)
for n in output:
    print(n)

The ‘file’ here is simulated, rater than being read and the ‘numbers’ are held in string objects, but both of those points can be addressed, if needs be.

To add: as per the comment by @steven.rumbalski:

- if item.find(':') > 0:
+ if ':' in item:

I think this is better spelled

if ':' in item:
1 Like

I think regular expressions would be appropriate here.

import re

with open('yourfile.txt', 'r') as f:
    result = re.findall(r'(\d+(?:\.\d+){0,1})', f.read())

Here’s an explanation of how this regular expression works.

Edit: Changed regex from (\d+(?:\.\d){0,1}) to (\d+(?:\.\d+){0,1}) to capture all of numbers past the decimal like 32.589 rather than capturing 32.5 and 89.

Thank you. The program is printing only the integer values splitted by :. I need to extract the float numbers also. And I have to read these datas from a file.

You’re welcome.

Between the two replies that you have and a little work from yourself, that’s doable.

Why not give it a go and post back, with your code (formatted, of course) if you hit a wall.

Thank you.
The program is printing all the numbers.

I will post the code after completion.

regards
Saradindu

My posted solution does both.

yes . Thank you. But it is printing data only upto one decimal.

like , for 76.30 it is printing like 76.3 , 0

Thank you.

Actually in my file there are points with upto 2 decimal numbers.

I sorted out the problem. Instead of \d i have to use \d+

Thank you