简体   繁体   English

Python如何删除文本文件中特定字符串之后或之前的特定行数

[英]Python how to delete a specific amount of lines after or before specific string in text file

All I can find is how to delete all lines after a specific word.我所能找到的就是如何删除特定单词后的所有行。 But I only want a specific amount of deleted lines.但我只想要特定数量的删除行。

For example I have a file that contains:例如,我有一个包含以下内容的文件:

FCT
Paris
105,4
35
2,161 million
LZQ
London
1572
11
8,982 million
PRI
Paris
105,4
35
2,161 million
Rome
1285
11
2,873 million
PRI
Paris
105,4
35
2,161 million

And now I want to delete 3 lines after Paris, the line before Paris and the line containing Paris itself.现在我想删除巴黎之后的 3 行,巴黎之前的行和包含巴黎本身的行。

Expected output would be:预期输出将是:

LZQ
London
1572
11
8,982 million

What works to delete only Paris:只删除巴黎的方法:

bad_words = ['Paris',]

with open('DataSystem.txt') as oldfile, open('newfile.txt', 'w') as newfile:
for line in oldfile:
    if not any(bad_word in line for bad_word in bad_words):
        newfile.write(line)

This is pretty inelegant but it works, assuming you want to remove exactly one previous line and exactly three following lines if a "bad word" is encountered.这很不优雅,但它有效,假设您想在遇到“坏词”时正好删除前一行和后三行 It will not work as intended if there are sometimes more lines or fewer lines following a "bad word":如果有时在“坏词”后面有更多行或更少行,它将无法按预期工作:

bad_words = {"Paris"}  # membership tests with sets are O(1)


with open('DataSystem.txt') as oldfile:
    data = oldfile.read().split("\n")


i = 0
new_data = []
while i < len(data):
    item = data[i]
    if item in bad_words:
        del new_data[-1]
        i += 4
        continue
    new_data.append(item)
    i += 1

Output:输出:

['LZQ',
 'London',
 '1572',
 '11',
 '8,982 million',
 'Rome',
 '1285',
 '11',
 '2,873 million']

You can then write this to your newfile :然后您可以将其写入您的newfile

with open('newfile.txt', 'w') as newfile:
    newfile.write("\n".join(new_data))

This does just what I described.这正是我所描述的。 Read the file in 5 lines at a time.一次读取 5 行文件。 If no "bad word" is found in line 2, write those 5 lines out.如果在第 2 行中没有发现“坏词”,请写出这 5 行。

bad_words = ['Paris']

with open('DataSystem.txt') as oldfile, open('newfile.txt', 'w') as newfile:
    while True:
        lines = [oldfile.readline() for _ in range(5)]
        if not lines[0]:
            break
        if lines[1].rstrip() not in bad_words:
            newfile.write( ''.join(lines) )
  • Since the end of data must contain million , you can try this code.由于数据的末尾必须包含million ,您可以尝试此代码。

example code:示例代码:

bad_words = ['Paris',]

with open('DataSystem.txt') as oldfile, open('newfile.txt', 'w') as newfile:
    lines = oldfile.readlines()
    temp = []
    is_bad = False
    for line in lines:
        temp.append(line)
        for bad_word in bad_words:
            if bad_word in line:
                is_bad = True
                break
        if "million" in line:
            if not is_bad:
                for new_data in temp:
                    newfile.write(new_data)
            is_bad = False
            temp = []

result:结果:

LZQ
London
1572
11
8,982 million
Rome
1285
11
2,873 million

暂无
暂无

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

相关问题 如何从 python 中的文本文件中删除特定行 - How to delete specific lines from a text file in python 如何在python中的文本文件中删除特定单词之前的一行 - How to delete one line before a specific word in a text file in python 如何检查句子特定部分之前的字符串是否与其他行中的任何文本匹配或不匹配(与特定部分之后相同)? - how to check whether the string before specific part of a sentence matches with any of the text in other lines or no (same with after specific part)? 我必须删除文本文件中的特定行 - I have to delete specific lines in text file 如何从文本文件中将特定数量的行输入到数组中,并重复该操作,直到使用完所有行 - How to input an specific amount of lines from a text file into an array and repeat the action untill all lines are used 如何删除特定字符之前的文本-Python(Pandas) - How to delete text before a specific character - Python (Pandas) 如何从文件中删除行直到特定字符? [Python] - How to delete lines from a file until a specific character? [Python] 在Python中特定出现的字符之前删除文本 - Delete text before the specific occurrence of a character in Python 如何通过 PYTHON 删除特定字符串前上一行的逗号 - How to delete the commas at the previous line before specific string by PYTHON 如何在python的文本文件中插入另一行之前的特定行,而不在任何行之间插入任何空行? - how to insert an specific line before another line, into a text file with python and without inserting any empty lines between any lines?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM