如何提取文本中间的块。
示例:
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 :(得分:0)
为什么不使用j=t.find(end)
代替j=t.startswith(end)
? Startswith在此示例中不起作用,因为end
字符串可能以比您键入的更多空格开头。您可以更好地匹配正则表达式(查看import re
),但find
似乎在这里完成工作。
答案 1 :(得分: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,