我有一个文件,我想阅读它的一个特定部分。这是文件。
..... ..... 管理服务器界面开始 ... .... .... .... .... 管理服务器界面结束 .... ....
我想在'admin server interface starts'到'admin server interface ends'之间读取文件的一部分。我在perl中找到了一种方法,但是在python中找不到方法。
in perl
while (<INP>)
{
print $_ if(/^AdminServer interface definitions begins/ .. /^AdminServer interface definitions ends/);
}
请不要帮忙。
答案 0 :(得分:3)
您可以逐行阅读文件并收集标记之间的内容。
def dispatch(inputfile):
# if the separator lines must be included, set to True
need_separator = True
new = False
rec = []
with open(inputfile) as f:
for line in f:
if "Admin server interface begins" in line:
new = True
if need_separator:
rec = [line]
else:
rec = []
elif "Admin server interface ends" in line:
if need_separator:
rec.append(line)
new = False
# if you do not need to process further, uncomment the following line
#return ''.join(rec)
elif new:
rec.append(line)
return ''.join(rec)
即使输入文件不包含结束分隔符(Admin server interface ends
),上面的代码也会成功返回数据。如果要捕获此类文件,可以使用条件修改最后return
:
if new:
# handle the case where there is no end separator
print("Error in input file: no ending separator")
return ''
else:
return ''.join(rec)
答案 1 :(得分:1)
如果文件不是很大并且你不关心内存消耗,你可以编写这个简单的解决方案:
from os.path import isfile
def collect_admin_server_interface_info(filename):
""" Collects admin server interface information from specified file. """
if isfile(filename):
contents = ''
with open(filename, 'r') as f:
contents = file.read()
beg_str = 'Admin server interface begins'
end_str = 'Admin server interface ends'
beg_index = contents.find(beg_str + len(beg_str))
end_index = contents.find(end_str)
if beg_index == -1 or end_index == -1:
raise("Admin server interface not found.")
return contents[beg_index : end_index]
else:
raise("File doesn't exist.")
此方法将尝试返回包含管理员服务器接口信息的单个字符串。