martin
martin

Reputation: 85

How to to find a value within two delimiter with Python 2.7

I have a file with many lines like this:

...

Model Path/IOSU/SIQER/Instruments/Sca/Breaking element/voltageZ/Out
Model/BDP/Simulator Replica/KO30_V/Value 

...

I need to find out the next last value within the delimiter "/". Ex for these two lines it should be: "voltageZ" and "KO30_V".

Ex:

with open('tt.txt') as fp:
    for line in fp:
        temp = #remove all after the last '/' 
        temp = #remove all before the next last '/'  
        print temp

This should print out:

voltageZ
KO30_V

How to do this? Could I use split for this? I'm using Python 2.7

Thx!

Upvotes: 0

Views: 18

Answers (1)

Haochen Wu
Haochen Wu

Reputation: 1823

print line.split('/')[-2] should work.

Upvotes: 1

Related Questions