简体   繁体   English

如果下一行包含特定字符串,则在文件的新行中添加字符串

[英]Adding string in new line of file if next line contains a specific string

I have tried different solutions to solve this but none of them worked and were too messy to post here.我尝试了不同的解决方案来解决这个问题,但没有一个有效,而且太乱了,无法在此处发布。 So I will just present my problem.所以我只会提出我的问题。 I have a .txt file that looks like this:我有一个如下所示的.txt文件:

Field1:
Something
Field2:
Something
Field3:
Field4:
Field1:
Something
Field2:
Field3:
Something
Field4:
Something
...

The file contains 4 fields which repeat themselves an unspecified number of times but it always ends with Field4 .该文件包含 4 个字段,这些字段重复了未指定的次数,但始终以Field4 Each field either has a string written under it or does not.每个字段下面都有一个字符串,或者没有。 Whether a field has something written under it or not is also random.一个字段下面是否写有内容也是随机的。 In case it does not, I have to insert a string underneath which says "Empty" .如果没有,我必须在下面插入一个字符串,上面写着"Empty" So in the end it should look something like this:所以最后它应该看起来像这样:

Field1:
Something
Field2:
Something
Field3:
Empty
Field4:
Empty
Field1:
Something
Field2:
Empty
Field3:
Something
Field4:
Something
...

My thought process was to open the original text file as readable and another text file as writable, iterate through the lines of the original and write each line in the output file.我的想法是将原始文本文件打开为可读,将另一个文本文件打开为可写,遍历原始文件的行并将每一行写入 output 文件。 If a line contains Field1 and the next line contains Field2 , then add string Empty underneath Field1 and continue doing this for each line.如果一行包含Field1并且下一行包含Field2 ,则在Field1下面添加字符串Empty并继续对每一行执行此操作。

Since text files cannot be edited in the middle, the program reads every line in readable.txt and append them to writable.txt with correcting lines.由于无法在中间编辑文本文件,因此程序会读取 readable.txt 中的每一行,然后 append 将它们读取到 writable.txt 并更正行。

file = open("readable.txt","r")
file = file.readlines()
f = open("writable.txt", "a") 
n = 0

while n < len(file):
   if "Field" in file[n]:
       f.write(str(file[n]))

       if "Field" in file[n + 1]:
           f.write("Empty\n") 
           n = n + 1 
           continue
       else:
           f.write(file[n + 1]) 
           n = n + 1 
           continue
   else:
       n = n + 1
       continue

file.close()
f.close()

If you have a large file, you don't want to read it all into memory before processing it, so you can do it line-by-line.如果你有一个大文件,你不想在处理它之前将它全部读入 memory,所以你可以逐行进行。

First, we can define a regex pattern to match the word "Field" , followed by any number of digits, followed by a colon.首先,我们可以定义一个正则表达式pattern来匹配单词"Field" ,后跟任意数量的数字,再后跟一个冒号。 Try the regex尝试正则表达式

Each time you read a line, if the previous line matches this pattern and the current line also matches the pattern, you write an "Empty" before writing this line.每次你读一行,如果前一行匹配这个模式并且当前行也匹配这个模式,你在写这行之前写一个"Empty" If not, you just write this line:如果没有,你只需写下这一行:

import re

pattern = re.compile(r"Field\d+:")  # Field, followed by one or more digits (\d+), and a colon

with open("in.txt") as infile, open("out.txt", "w") as outfile:
    prev_line = ""
    for line in infile:
        if pattern.match(line) and pattern.match(prev_line):
            outfile.write("Empty\n") # Write an Empty line if both lines match the pattern:

        outfile.write(line) # This is outside an if because we always write the current line
        prev_line = line

With your input file, this gives:使用您的输入文件,这将给出:

Field1:
Something
Field2:
Something
Field3:
Empty
Field4:
Empty
Field1:
Something
Field2:
Empty
Field3:
Something
Field4:
Something

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

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