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

-sid

Aulis Ronkainen
  • 200
  • 1
  • 3
  • 10
  • 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 09 '12 at 19:33

2 Answers2

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]
Tom77
  • 844
  • 4
  • 11
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

Jason Ward
  • 1,155
  • 9
  • 7
  • 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 06:37