简体   繁体   English

合并2个文件以创建1个文件作为输出

[英]Combining 2 files to create 1 file as output

I got 2 documents which look likes: First: 我有2个看起来像的文件:首先:

port2
port4
port10
etc.

Second: 第二:

port1
some stuff
about the port
I do not need
!
port2
some stuff
about the port
I really need
!
some generic stuff which is completely useless
!
port3
some stuff
about the port
I do not need
!
port4
some stuff
about the port
I really need
!
etc

Now, what I want is to create a loop that for each line in the first document we'll go through the second document and create a new file which contains all the data I need ("port2" until "!", "port4" until "!" etc) 现在,我要创建一个循环,对于第一个文档中的每一行,我们将遍历第二个文档,并创建一个包含我需要的所有数据的新文件(“ port2”,直到“!”,“ port4”直到“!”等)

What I got so far: 我到目前为止所得到的:

def access():
with open ("D:/portlist.txt") as f1, open ("D:/config.txt") as f2:
    match = False
    for line in f1:
        newConfig = open ("D:/portconfig.test.txt", "a")
        interface = line
        for line2 in f2:
            if re.match(interface, line2):
                newConfig.write(line2)
                print(line2)
                match = True
            elif re.match("!", line2):
                match = False   
            elif match:
                newConfig.write(line2)
        newConfig.close()   
access()

Problem is that the script stops after returning all about port2. 问题是脚本在返回所有有关port2的信息后停止。 It seems like the script doesn't return to the first loop to continue the process. 脚本似乎没有返回第一个循环以继续该过程。 Any ideas? 有任何想法吗?

Your problem stems from the fact that once a file is read to the end, it doesn't automatically seek back to the first point. 您的问题源于以下事实:一旦读取完文件,它就不会自动返回到第一点。 Since you're looping over the second file for each of the values you search, you either need to seek back via f2.seek(0) , or simply read the file's content in memory only once and then loop on that. 由于要遍历第二个文件以查找每个值,因此您要么需要通过f2.seek(0) ,要么只需读取一次文件在内存中的内容,然后对其进行循环。

About your code a quick (and dirty) solution exploits the fact that you have block separators ( ! ): 关于您的代码,一种快速(又肮脏)的解决方案利用了您拥有块分隔符( ! )的事实:

with open(...) as f1, open(...) as f2:
  section_names_to_keep = f1.read().splitlines()
  config_content = f2.read()

config_blocks = config_content.split('!\n')
blocks_to_keep = [ bl for bl in config_blocks if bl.splitlines()[0] in section_names_t_keep ]

with open('your_output_file.txt', 'a') as fp:
  fp.write('!\n'.join(blocks_to_keep))

Note: You weren't too clear on the expected output format, so I assume it should look like config.txt . 注意:您对预期的输出格式不太清楚,所以我认为它应该看起来像config.txt I write to the output file all at once (first I generate the output content in memory with '!\\n'.join(blocks_to_keep) and then it gets written out). 我一次写入所有输出文件(首先我在内存中使用'!\\n'.join(blocks_to_keep)生成输出内容,然后将其写出)。 If (as I'm assuming) your data is small, this won't be an issue. 如果(我假设)您的数据很小,那么这将不是问题。 If that's not the case, just loop over blocks_to_keep and write out block by block. 如果不是这种情况,则只需遍历blocks_to_keep并逐块写出即可。

Small code explanation: 小代码说明:

In the first block I simply load the content of both files. 在第一步中,我只是加载两个文件的内容。 Since for the first file we're interested in each line, I already split it over the lines. 由于对于第一个文件,我们对每一行都感兴趣,因此我已经将其拆分为各行。

In the second block, I split the config over the block separator !\\n and then I filter the list of blocks keeping only those blocks whose first line is in the list we got from the first file. 在第二个块中,我将配置拆分到块分隔符!\\n ,然后过滤块列表,仅保留第一行位于从第一个文件获得的列表中的那些块。

The third block is just output. 仅输出第三块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM