2014-11-24 197 views
0
[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] 
+0

如果你有指標,size.You可以這樣做'一個[指數:指數+大小]' – 2014-11-24 11:23:50

+0

@AshwaniDausodia在最終產量將取決於我輸入更改數字。所以我不知道他們會有多大。 – Johnny 2014-11-24 11:25:39

+0

數據似乎是'list'而不是'string'請清除你的問題? – 2014-11-24 11:27:54

回答

1

您可以嘗試下面的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' 
0

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

awk '{print $14}' file 

您可以過濾掉不具有至少14列,分別都行語法:

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

你試過使用正則表達式嗎?我建議的模式:

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

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

0

如果每個這些行作爲一個單獨的字符串,可以提取的最後一個子該線路爲:

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

如果

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

然後用

x[-1] 

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

1

您可以簡單地使用str.splitstr.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 
+0

@johnny請檢查解決方案,我更新。 – 2014-11-24 11:49:55

相關問題