Python 保存文本文件的一部分
我使用Rancid服务器在网络上保存有关Cisco交换机的信息。我想编写一个Python3脚本,将数据的配置部分提取到文本文件中。我已经让这个工作,但我有一个文件,其中有两个配置中,我只想要第一个配置 这就是我所做的:Python 保存文本文件的一部分,python,python-3.x,text-files,Python,Python 3.x,Text Files,我使用Rancid服务器在网络上保存有关Cisco交换机的信息。我想编写一个Python3脚本,将数据的配置部分提取到文本文件中。我已经让这个工作,但我有一个文件,其中有两个配置中,我只想要第一个配置 这就是我所做的: import sys flag = False start = ('version', 'config-register') end = ('@\n', 'monitor 6\n', 'end\n') with open(sys.arbv[1], "r") as file:
import sys
flag = False
start = ('version', 'config-register')
end = ('@\n', 'monitor 6\n', 'end\n')
with open(sys.arbv[1], "r") as file:
for line in file:
if line.startswith(start):
file = True;
if flag:
sys.stdout.write(line)
if line.endswith(end):
flag = False
具有两次配置的文件使用'version'
作为开始,使用'@\n'
作为结束。我曾尝试使用中断
,但我仍然得到这两种配置
文件示例:
!VTP:VTP域名:
!VTP:VTP修剪模式:禁用(操作禁用)
!VTP:VTP V2模式:禁用
!VTP:VTP陷阱生成:禁用
!VTP:MD5摘要:0x05 0xBB 0x45 0x03 0x57 0xBE 0xBA 0x57
!VTP:正在运行的VTP版本:1
!
!调试:调试级别设置为次要(1)
!调试:新会话的默认日志记录级别:3
!
!核心:模块实例进程名称PID日期(年-月-日时间)
!芯材:-----------------------------------------------------
!
!进程日志:进程PID正常退出堆栈核心日志创建时间
!过程日志:-----------------------------------
!
版本和配置寄存器的顺序在这里很重要。因此,您最好只对文件进行两次迭代。通过这种方式,您可以将需要查找的文件分区分成多个组。一次查找版本值,一次查找配置寄存器
import sys
flag = False
VERSION_NAME = r'version'
CONFIG_NAME = r'config-register'
end = ('@\n', 'monitor 6\n', 'end\n')
FILE_NAME = sys.argv[1]
# find the config-register
with open(FILE_NAME, "r") as file:
start = CONFIG_NAME
config_found = False
for line in file:
if line.startswith(start) and not config_found:
flag = True # correction
if flag:
sys.stdout.write(line)
if flag and line.endswith(end):
print("Found Break")
break # correction
if line.startswith(start):
config_found = True
# find the version
with open(FILE_NAME, "r") as file:
start = VERSION_NAME
config_found = False
for line in file:
if line.startswith(start) and not config_found:
flag = True
if flag:
sys.stdout.write(line)
if flag and line.endswith(end):
print("Found Break")
break
if line.startswith(start):
config_found = True
然后再次循环,只搜索配置版本和“end”。这不是最注重性能的,但由于您没有处理整个文件,希望这能满足您的需要。版本和配置寄存器的顺序在这里很重要。因此,您最好只对文件进行两次迭代。通过这种方式,您可以将需要查找的文件分区分成多个组。一次查找版本值,一次查找配置寄存器
import sys
flag = False
VERSION_NAME = r'version'
CONFIG_NAME = r'config-register'
end = ('@\n', 'monitor 6\n', 'end\n')
FILE_NAME = sys.argv[1]
# find the config-register
with open(FILE_NAME, "r") as file:
start = CONFIG_NAME
config_found = False
for line in file:
if line.startswith(start) and not config_found:
flag = True # correction
if flag:
sys.stdout.write(line)
if flag and line.endswith(end):
print("Found Break")
break # correction
if line.startswith(start):
config_found = True
# find the version
with open(FILE_NAME, "r") as file:
start = VERSION_NAME
config_found = False
for line in file:
if line.startswith(start) and not config_found:
flag = True
if flag:
sys.stdout.write(line)
if flag and line.endswith(end):
print("Found Break")
break
if line.startswith(start):
config_found = True
然后再次循环,只搜索配置版本和“end”。这不是最注重性能的部分,但由于您没有处理整个文件,希望这能满足您的需要。添加实际内容,开始和结束没有意义,该部分如何以两个不同的字符串开始,以三个不同的字符串结束?我有三种不同类型的开关,两个文件具有字符串“config register”,而第三个文件具有“version”字符串。每个文件都有一个单独的字符串,配置在该字符串的结尾处。开始变量是程序开始处理并在结束变量处停止的位置。如果您提供一个示例,将更容易帮助您确定我必须删除一些信息,但以下是一个示例,如果文件:***文件的开始****版本5.2(1)N1(4)日志级别功能管理器0主机名无功能telnet功能tacacs+cfs eth分发功能udld功能接口vlan功能lacp功能vpc功能lldp功能vtp功能fex用户名(已删除)添加实际内容,开始和结束没有意义,该节如何以两个不同的字符串开始,以三个不同的字符串结束?我有三种不同类型的开关,两个文件有字符串“config register”,而第三个文件有“version”字符串。每个文件都有一个单独的字符串,配置在该字符串的结尾处。开始变量是程序开始处理并在结束变量处停止的位置。如果您提供一个示例,将更容易帮助您确定我必须删除一些信息,但以下是一个示例,如果文件:***文件的开始****版本5.2(1)N1(4)日志级别功能管理器0主机名无功能telnet功能tacacs+cfs eth分发功能udld功能接口vlan功能lacp功能vpc功能lldp功能vtp功能fex用户名(已删除)添加实际内容,开始和结束没有意义,该节如何以两个不同的字符串开始,以三个不同的字符串结束?我有三种不同类型的开关,两个文件有字符串“config register”,而第三个文件有“version”字符串。每个文件都有一个单独的字符串,配置在该字符串的结尾处。开始变量是程序开始处理并在结束变量处停止的位置。如果您提供一个示例,将更容易帮助您确定我必须删除一些信息,但以下是一个示例,如果文件:***文件的开始****版本5.2(1)N1(4)日志级别功能管理器0主机名无功能telnet功能tacacs+cfs eth分发功能udld功能接口vlan功能lacp功能vpc功能lldp功能vtp功能fex用户名(已删除)