Hi,

I get a the following as output after executing a command remotely:
for example;

No of records Date Status
1 7/7/2010 Progress
Total Number of records Processed so far: 1012
Currently active records: 1

Now evidently Python treats this out put as a string.
Now my problem is how to split this string in to words.
Precisely I need to read the Currently active records: 1 and then initialize a variable as:
Active_records = 1.

How do I do this?
Any help be would highly appreciated.

Regards,
Prashanth

Lazy simple mode.

f_in = open('output.txt').readlines()

for line in f_in:
    if 'Currently active records' in line:
        _, _, _, active_records = line.split(' ')
        print active_records

Looks the number is at end of string so you could do:

>>> output='bnu kn uhx dwtjp 1'
>>> output.rsplit(' ',1)
['bnu kn uhx dwtjp', '1']
>>> int(output.rsplit(' ',1)[-1])
1

Extracting the number at the end of a data string is fairly simple ...

data = """\
No of records Date Status
1 7/7/2010 Progress
Total Number of records Processed so far: 1012
Currently active records: 1"""

# split into a list of words and take the final element
active_records = data.split()[-1]
print(active_records)  # 1

Extracting the number at the end of a data string is fairly simple ...

data = """\
No of records Date Status
1 7/7/2010 Progress
Total Number of records Processed so far: 1012
Currently active records: 1"""

# split into a list of words and take the final element
active_records = data.split()[-1]
print(active_records)  # 1

As long as data is not very long this approach is ok. If you want to save computers efforts and little execution time you can use rsplit with second argument 1. or here for completenes rpartition:

data = """\
No of records Date Status
1 7/7/2010 Progress
Total Number of records Processed so far: 1012
Currently active records: 1"""

# split into a list of words and take the final element
_,_,active_records = data.rpartition('Currently active records: ')
active_records = int(active_records) ## gives error if something after integer
print(active_records)  # 1

If the string would be in middle with some same elements bracketing it, you could use my between function, which I posted as code snippet some time ago.

Tony rpartition soultion timeit 1000000 loops average 5.92587408782
Vega data.split()[-1] soultion 1000000 loops average 5.90504511194

Not so much differnce but vega solution is a little faster,and simpler to read and understand.
That count as a plus.

Extracting the number at the end of a data string is fairly simple ...

data = """\
No of records Date Status
1 7/7/2010 Progress
Total Number of records Processed so far: 1012
Currently active records: 1"""

# split into a list of words and take the final element
active_records = data.split()[-1]
print(active_records)  # 1

Nice, looks like I wasn't lazy enough.

Nice line Vegas.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.