簡體   English   中英

獲取字符串中的最后一個子字符串?

[英]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

您將如何獲得最后一個子字符串“ crop = 400:704:440:8”字符串? 我能想到的是使用rfind(“ crop =”)來獲取初始索引,但是我不確定下一步該怎么做?

我的解決方案:

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

print output[start:end]

您可以嘗試下面的re.findall函數。

>>> 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'

您可以將str.splitstr.startswith與索引str.startswith使用。

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

您可以使用awk打印第n列:

awk '{print $14}' file

您可以使用以下語法過濾掉沒有至少14列的所有行:

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

您是否嘗試過使用正則表達式? 我建議模式:

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

您可以使用http://regex101.com/構建自己的新網站。

如果您將每行都作為單獨的字符串,則可以將該行的最后一個子字符串提取為:

# 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]

如果

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

然后使用

x[-1]

它將返回字符串中的最后一個字符

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM