2016-03-27 36 views
1

如何提取位於文本中間的塊。如何提取位於文件中文本中間的塊

例子:

User authority 


-------------- 


       Subscriber number = 189159 


      Call-out authority = Intra-office 


           = Local 


           = Local toll 


           = National toll 


           = International toll 


           = Intra-Centrex 


           = Outgoing Centrex 


           = Intra-office national toll 


           = Intra-office international toll 


           = Intra-centrex local toll 


           = Intra-centrex national toll 


           = Intra-centrex international toll 


           = Intra-office local toll 


           = Customize call-out authority 1 


           = Customize call-out authority 2 


           = Customize call-out authority 3 


           = Customize call-out authority 4 


           = Customize call-out authority 7 


           = Customize call-out authority 9 


           = Customize call-out authority 10 


           = Customize call-out authority 11 


           = Customize call-out authority 12 


           = Customize call-out authority 13 


           = Customize call-out authority 14 


           = Customize call-out authority 15 


           = Customize call-out authority 16 


       Call-in authority = Intra-office 


           = Local 


           = Local toll 


           = National toll 


           = International toll 


           = Intra-Centrex 


           = Incoming Centrex 


           = Intra-office national toll 


           = Intra-office international toll 


           = Intra-centrex local toll 


           = Intra-centrex national toll 


           = Intra-centrex international toll 


           = Intra-office local toll 


           = Customize call-in authority 1 


           = Customize call-in authority 2 


           = Customize call-in authority 3 


           = Customize call-in authority 4 


           = Customize call-in authority 5 


           = Customize call-in authority 6 


           = Customize call-in authority 7 


           = Customize call-in authority 8 


           = Customize call-in authority 9 


           = Customize call-in authority 10 


           = Customize call-in authority 11 


           = Customize call-in authority 12 


           = Customize call-in authority 13 


           = Customize call-in authority 14 


           = Customize call-in authority 15 


           = Customize call-in authority 16 


     Call-in Barring Authority = NULL 


         K value = K0 

我只是想提取與呼出權限=局內開始塊直到叩除非管理局= NULL

這是我的代碼:

begin='    Call-out authority =' 
end='  Call-in Barring Authority =' 

with open("data.txt", "r") as f: 
    t = f.read() 
    i = t.find(begin) 
    j=t.startswith(end) 
    print(t[i:j]) 

data.txt是文件。

謝謝大家。

回答

0

爲什麼不使用j=t.find(end)而不是j=t.startswith(end)?在此示例中,Startswith不起作用,因爲end字符串可能以比輸入更多空格開始。你可以用正則表達式更好地匹配(看看import re),但find似乎在這裏做它的工作。

0

你可以簡單地逐行讀取,直到找到begin然後直到你找到end

begin='    Call-out authority =' 
end='  Call-in Barring Authority =' 

with open("data.txt", "r") as f: 
    for t in f: 
     if t.startswith(beg): break 
    print t 
    for t in f: 
     if t.starstwith(end): break 
     print t, 
相關問題