0

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


  • 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
1

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]
  • 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
0
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

  • 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

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