Johnny
Johnny

Reputation: 127

Get the last substring in string?

[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:638638 t:10.643967 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:640640 t:10.677333 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:642642 t:10.710700 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:644644 t:10.744067 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:646646 t:10.777433 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:648648 t:10.810800 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:650650 t:10.844167 crop=400:704:440:8
frame=  326 fps=0.0 q=0.0 Lsize=N/A time=00:00:10.89 bitrate=N/A    
video:31kB audio:1876kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown

How would you get the last substring "crop=400:704:440:8" string? What I can think of is using rfind("crop=") to get the initial index but I'm not sure what i would do next?

My solution:

start =  output.rfind("crop=")
end =  output.find('\n', start, len(output))

print output[start:end]

Upvotes: 0

Views: 150

Answers (6)

Vishnu Upadhyay
Vishnu Upadhyay

Reputation: 5061

You can simply use str.split and str.startswith with indexing.

a = """[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:638638 t:10.643967 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:640640 t:10.677333 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:642642 t:10.710700 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:644644 t:10.744067 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:646646 t:10.777433 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:648648 t:10.810800 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:650650 t:10.844167 crop=400:704:440:8
frame=  326 fps=0.0 q=0.0 Lsize=N/A time=00:00:10.89 bitrate=N/A    
video:31kB audio:1876kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown"""

for line in a.split('\n'):
    if line.split()[-1].startswith('crop'):
        print line.split()[-1] 


>>> 
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8
crop=400:704:440:8

Upvotes: 1

Avinash Raj
Avinash Raj

Reputation: 174696

You could try the below re.findall function.

>>> import re
>>> s = """[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:638638 t:10.643967 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:640640 t:10.677333 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:642642 t:10.710700 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:644644 t:10.744067 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:646646 t:10.777433 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:648648 t:10.810800 crop=400:704:440:8
[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:650650 t:10.844167 crop=400:704:440:8
frame=  326 fps=0.0 q=0.0 Lsize=N/A time=00:00:10.89 bitrate=N/A    
video:31kB audio:1876kB subtitle:0kB other streams:0kB global headers:0kB muxing overhead: unknown"""
>>> re.findall(r'crop=[\d:]+', s)[-1]
'crop=400:704:440:8'

Upvotes: 1

Hana Bzh
Hana Bzh

Reputation: 2260

if

x = "crop=400:704:440:8"

then use

x[-1]

it will return the last character in the string

Upvotes: 0

ronrest
ronrest

Reputation: 1222

If you have each of those lines as a separate string, you can extract the last substring for that line as:

# STRING FOR THAT LINE
s = """[Parsed_cropdetect_0 @ 0x7f8ee9c22c60] x1:438 x2:841 y1:0 y2:718 w:400 h:704 x:440 y:8 pts:650650 t:10.844167 crop=400:704:440:8"""

# EXTRACT LAST SUBSTRING
s.split()[-1]

Upvotes: 0

Abs
Abs

Reputation: 100

Did you try using regex? I would suggest the pattern :

((crop=)(\d|:)*(\d))

You may use http://regex101.com/ to build your own new one.

Upvotes: 0

qstebom
qstebom

Reputation: 739

You can use awk to print the n:th column:

awk '{print $14}' file

You can filter out all lines that does not have at least 14 columns with the following syntax:

awk 'NF==14 {print $14}' file

Upvotes: 0

Related Questions