简体   繁体   English

使用 Python 查找字符串并删除从匹配字符串到文件末尾的所有行

[英]Using Python find a string and delete all lines from the matched string to the end of the file

My Google skills may be lacking, but I'm surprised that I've not been able to find anything that exactly matches what I am trying to do here in Python (it has to be in Python).我的 Google 技能可能有所欠缺,但令我惊讶的是,我在 Python 中找不到与我在此处尝试执行的操作完全匹配的内容(它必须在 Python 中)。

I have a lot of plain text files (600+), some of these files contain a section I want to delete.我有很多纯文本文件(600+),其中一些文件包含我要删除的部分。 The section always starts with a line containing a Markdown header: ###### Backlinks .该部分始终以包含 Markdown header: ###### Backlinks的行开头。 I'm trying to find the first line of that section, delete it, and then also delete all the lines after it, all the way until the end of the file.我试图找到该部分的第一行,删除它,然后删除它之后的所有行,一直到文件末尾。

A few points to clarify:有几点需要澄清:

  • The section I want to delete always starts with the specific string: ###### Backlinks .我要删除的部分始终以特定字符串开头: ###### Backlinks
  • It is always the last section of the file.始终是文件的最后一部分。
  • I always want to delete from the start of the backlinks section to the end of the file.总是想从反向链接部分的开头删除到文件的结尾。

The plain text files always follow the below structure, but vary in size and length:纯文本文件始终遵循以下结构,但大小和长度各不相同:

---
note: 20200806151434
title: Vestibulum Ante
---

# Vestibulum ante

Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Suspendisse ultrices erat eu magna aliquet, vitae commodo felis porttitor.

###### Backlinks

- [[20200806151410]]
- [[20200806151414]]

I'm no expert with Python, and because I've not really found anything that really matches what I'm after, I'm not sure where to start.我不是 Python 的专家,因为我还没有真正找到与我所追求的东西真正匹配的东西,所以我不知道从哪里开始。 I thought it would be simpler than it currently seems to be.我认为它会比现在看起来更简单。

I'm using the following code so far, and I am able to find the ###### Backlinks string where it exists in each file I'm looping through.到目前为止,我正在使用以下代码,并且能够在我正在循环的每个文件中找到它存在的###### Backlinks字符串。 At the moment, I'm just printing the string, and the path to the file that contains the string, but as above I actually want to delete the ###### Backlinks line and all lines after it silently and safely (without running the risk of losing the content of any of the files).目前,我只是打印字符串,以及包含该字符串的文件的路径,但如上所述,我实际上想删除###### Backlinks行以及它之后的所有行,并且安全地(没有冒丢失任何文件内容的风险)。

Please let me know if you need me to clarify.如果您需要我澄清,请告诉我。 Any help or pointers will be much appreciated.任何帮助或指示将不胜感激。

path_notes = /home/user/notes

for note in path_notes:
    with open(note, "r") as note_content:
    
        # Find existing backlinks and get line number:
        for line in note_content:
            if line.__contains__("###### Backlinks"):
                print(line.strip("\n") + " " + note)

An easy way to remove the portion of the file that comes after "###### Backlinks" would be to loop through all lines and write them to the same file, just breaking the loop as soon as "###### Backlinks" appears.删除"###### Backlinks"之后的文件部分的一种简单方法是遍历所有行并将它们写入同一个文件,只要在"###### Backlinks"出现。

fname = "somefile.txt"
t = open(fname,"r")
lines = t.readlines()
t.close()

t = open(fname,"w") ## Caution! This overwrites the original file. Be sure to try this in a safe place.
i = 0
while i < len(lines) and not "##### Backlinks" in lines[i]:
    t.write(lines[i])
    i+=1
t.close()

This overwrites your file with all lines in the original file except anything that comes after "##### Backlinks" is encountered.这将用原始文件中的所有行覆盖您的文件,除了遇到“##### Backlinks”之后的任何内容。

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

相关问题 使用Python从文本文件读取的行中的特殊结束行字符/字符串 - Special End-line characters/string from lines read from text file, using Python 从文件python中删除字符串和字符串之前的所有行 - remove string and all lines before string from file python Spark在csv文件python的所有行之间找到特定的字符串 - Spark Find a particular string between all lines of a csv file python 删除文件中的所有行,直到与字符串一致 - Delete all lines in file until line with string python正则表达式查找匹配的字符串 - python regex find matched string 找到匹配字符串或正则表达式的结束偏移量 - Find the end offset of a matched string or regex python-在匹配的字符串后提取几行 - python - extract several lines following a matched string 在python中将行写入匹配的字符串 - Write lines up to matched string in python 搜索开始字符串和搜索结束字符串,然后在python中打印开始到结束行之间的所有行 - search begin string and search end string then print all lines between begin to end lines in python 我正在尝试编写 python 脚本以在文件中查找超过 1000 行的字符串,并在该字符串匹配后删除几行 (10) - I am trying to write python script to find a string in file with more than 1000 lines and delete few lines (10) after that string match
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM