Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

I am a beginner in writing Python scripts and I need help on the following:

Threads::num,47141,47146,47151,47156,47161,47166,47171,47176

How can I get and display the last value '47176'

The following is the part of the code I have written:

elif sys.argv[1] == "-c": b = sys.argv[2] with open(b) as f: for line in f: if 'Threads::num' in line: print line.strip(',').split(',')[-1] print line

The output of this code is that it displays all the values as follows Threads::num,47141,47146,47151,47156,47161,47166,47171,47176

Please do help me

thx,

sid


share|improve this question
    
Posts on this corner of the SE network are expected to relate to software testing and/or quality assurance in some way. This post fails to meet that criterion. –  corsiKa Jul 9 '12 at 19:33
add comment

closed as off topic by corsiKa Jul 9 '12 at 19:09

Questions on Software Quality Assurance & Testing Stack Exchange are expected to relate to software quality assurance or testing within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

import sys

if sys.argv[1] == '-c':
    b = sys.argv[2]

with open(b) as f:
    for line in f:
        if line.startswith('Threads::num'):
            print line.split(',')[-1].strip()

Also I would suggest using optparse instead of sys.argv

share|improve this answer
    
Hi Jason thanks for the reply and I have solved the issue i have used I gave print split_threads_string[-2] as there was a newline involved at the end of the line. –  Siddharth Ravindran Jul 26 '11 at 6:37
add comment

Something like this:

threads_string = "Threads::num,47141,47146,47151,47156,47161,47166,47171,47176"
split_threads_string = threads_string.split(',')
print split_threads_string[-1]
share|improve this answer
    
Thanks Tom ill try this.. –  Siddharth Ravindran Jul 22 '11 at 14:53
    
I solved it: I gave print split_threads_string[-2] as there was a newline involved at the end of the line. –  Siddharth Ravindran Jul 26 '11 at 6:35
    
I would suggest trimming it before splitting. If the answe rabove is correct you should mark it as such. –  Ardesco Jul 26 '11 at 9:16
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.