Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/326.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python,循环文本,搜索块_Python_Loops_Text - Fatal编程技术网

Python,循环文本,搜索块

Python,循环文本,搜索块,python,loops,text,Python,Loops,Text,我正在编写一段python代码,用来自网络设备的各种show命令替换用于搜索文件的旧php代码。我将适当的值写入csv文件以生成类似cmdb的工作表。 我现在正试图通过文件搜索找到一个特定的文本块,并将其写入另一个临时文件。下面是一段代码: for root, dirs, _ in os.walk(cmdbDir): for d in dirs: dir = os.path.join(root, d) # Loop through all subdirectories w

我正在编写一段python代码,用来自网络设备的各种show命令替换用于搜索文件的旧php代码。我将适当的值写入csv文件以生成类似cmdb的工作表。 我现在正试图通过文件搜索找到一个特定的文本块,并将其写入另一个临时文件。下面是一段代码:

for root, dirs, _ in os.walk(cmdbDir):
for d in dirs:
    dir = os.path.join(root, d)
    # Loop through all subdirectories
    with os.scandir(dir) as it:
        for entry in it:
            if entry.name.endswith(".txt") and entry.is_file():
                if dir.endswith('juniper'):
                    manufacturer = 'Juniper'
                    flag = False
                    flag2 = False
                    with open(entry.path, 'r') as f:
                        for line in f:
                            # Write a temp file per Juniper Node
                            nodes = ['node0','node1']
                            for node in nodes:
                                tmpfile = (f'{tmpDir}\\{hostname}-{node}.txt')
                                temp_file = open(tmpfile, 'a')
                                if line.startswith(f'set groups {node}'):
                                    temp_file.write(line)

                                # Search for section show chassis hardware
                                if line.startswith('show chassis hardware'):
                                    flag = True
                                elif line.startswith('{primary:node0}'):
                                    flag = False
                                elif flag:
                                    if line.startswith(node):
                                        flag2 = True
                                    elif line.startswith('\n') or len(line) == 0:
                                        flag2 = False
                                    elif flag2:
                                        print(line)
                                        temp_file.write(line)
                                temp_file.close()
因此,它在目录中循环,并在所有子目录中查找扩展名为.txt的文件。对于这个特殊的Juniper设备,我想对node0和node1的数据进行grep,并将它们放在单独的文件中。当我只为1个节点运行这段代码时(因此我只将node0或node1作为参数传递),它工作得很好,因此grep文本的代码似乎还可以。当我尝试运行node0和node1的代码时,我认为设置标志有问题,然后我在两个文本文件中看到bot节点的输出

这是我循环浏览的原始文件的一部分:

show chassis hardware
node0:
--------------------------------------------------------------------------
Hardware inventory:
Item             Version  Part number  Serial number     Description
Chassis                                CV3016AF1127      SRX300
Routing Engine   REV 0x08 650-065039   CV3016AF1127      RE-SRX300
FPC 0                                                    FPC
  PIC 0                                                  6xGE,2xGE SFP Base PIC
Power Supply 0

node1:
--------------------------------------------------------------------------
Hardware inventory:
Item             Version  Part number  Serial number     Description
Chassis                                CV3016AF1125      SRX300
Routing Engine   REV 0x08 650-065039   CV3016AF1125      RE-SRX300
FPC 0                                                    FPC
  PIC 0                                                  6xGE,2xGE SFP Base PIC
Power Supply 0

{primary:node0}
因此,它首先查找“show chassis hardware”和{primary:node0}之间的文本,然后查找“node0”和空行之间的文本,node1的情况大致相同

这是仅使用node0运行此代码时的结果:

set groups node0 system host-name xxx
set groups node0 interfaces fxp0 unit 0 family inet address x.x.x.x/26
set groups node0 interfaces fxp0 unit 0 family inet address x.x.x.x/26 master-only
--------------------------------------------------------------------------
Hardware inventory:
Item             Version  Part number  Serial number     Description
Chassis                                CV3016AF1127      SRX300
Routing Engine   REV 0x08 650-065039   CV3016AF1127      RE-SRX300
FPC 0                                                    FPC
  PIC 0                                                  6xGE,2xGE SFP Base PIC
Power Supply 0
当我使用node0和node1运行它时:

set groups node1 system host-name xxx
set groups node1 interfaces fxp0 unit 0 family inet address x.x.x.x/26
set groups node1 interfaces fxp0 unit 0 family inet address x.x.x.x/26 master-only
node0:
--------------------------------------------------------------------------
Hardware inventory:
Item             Version  Part number  Serial number     Description
Chassis                                CV3016AF1127      SRX300
Routing Engine   REV 0x08 650-065039   CV3016AF1127      RE-SRX300
FPC 0                                                    FPC
  PIC 0                                                  6xGE,2xGE SFP Base PIC
Power Supply 0
--------------------------------------------------------------------------
Hardware inventory:
Item             Version  Part number  Serial number     Description
Chassis                                CV3016AF1125      SRX300
Routing Engine   REV 0x08 650-065039   CV3016AF1125      RE-SRX300
FPC 0                                                    FPC
  PIC 0                                                  6xGE,2xGE SFP Base PIC
Power Supply 0
我试图了解它哪里出了问题,我认为循环中出现了一些问题,某个var没有被取消设置或保持“活动”

我还尝试将这段代码放入函数中,但结果相同


提前感谢。

必须为每个节点分别设置flag2。为此,将line
nodes=['node0','node1']
向上移动到initial
flag2=False上方。然后将后一行替换为
flag2=dict.fromkeys(nodes,False)
,以后每次使用
flag2
替换为
flag2[node]

然后代码的相关部分看起来像(未测试):


必须为每个节点分别设置
flag2
。为此,将line
nodes=['node0','node1']
向上移动到initial
flag2=False上方。然后将后一行替换为
flag2=dict.fromkeys(nodes,False)
,以后每次使用
flag2
替换为
flag2[node]

然后代码的相关部分看起来像(未测试):


对于节点,“flag2”必须保持独立。使用字典(以节点名为键)或包含两个条目的列表。嗨,有代码示例吗?我如何使用它?或者我能用的参考资料?我是编程/python方面的新手,因此在何处使用什么函数非常具有挑战性。“flag2”必须与节点分开。使用字典(以节点名为键)或包含两个条目的列表。嗨,有代码示例吗?我如何使用它?或者我能用的参考资料?我是编程/python方面的新手,所以在哪里使用什么函数很有挑战性。
        if dir.endswith('juniper'):
            manufacturer = 'Juniper'
            nodes = ['node0','node1']
            flag = False
            flag2 = dict.fromkeys(nodes, False)
            with open(entry.path, 'r') as f:
                for line in f:
                    # Write a temp file per Juniper Node
                    for node in nodes:
                        tmpfile = (f'{tmpDir}\\{hostname}-{node}.txt')
                        temp_file = open(tmpfile, 'a')
                        if line.startswith(f'set groups {node}'):
                            temp_file.write(line)

                        # Search for section show chassis hardware
                        if line.startswith('show chassis hardware'):
                            flag = True
                        elif line.startswith('{primary:node0}'):
                            flag = False
                        elif flag:
                            if line.startswith(node):
                                flag2[node] = True
                            elif line.startswith('\n') or len(line) == 0:
                                flag2[node] = False
                            elif flag2[node]:
                                print(line)
                                temp_file.write(line)
                        temp_file.close()