Siddharth Ravindran
Siddharth Ravindran

Reputation: 47

How to get the last value in a list/string using Python

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

Upvotes: 0

Views: 789

Answers (1)

user208139
user208139

Reputation:

I think your post got re-formatted, because the Python interpreter definitely won't execute it, but take a look at this:

print line.strip(',').split(',')[-1] 
print line

The phrase line.strip(',').split(',')[-1] ends up as '47116', but then you do a print line which just outputs the whole of the contents of line again.

Upvotes: 1

Related Questions