简体   繁体   English

尝试删除文本文件中包含特定字符的行

[英]Trying to delete lines in a text file that contain a specific character

I'm testing the code below, but it doensn't do what I would like it to do.我正在测试下面的代码,但它没有做我希望它做的事情。

delete_if = ['#', ' ']
with open('C:\\my_path\\AllDataFinal.txt') as oldfile, open('C:\\my_path\\AllDataFinalFinal.txt', 'w') as newfile:
    for line in oldfile:
        if not any(del_it in line for del_it in delete_if):
            newfile.write(line)
print('DONE!!')

Basically, I want to delete any line that contains a '#' character (the lines I want to delete start with a '#' character).基本上,我想删除任何包含“#”字符的行(我想删除的行以“#”字符开头)。 Also, I want to delete any/all lines that are completely blank.另外,我想删除任何/所有完全空白的行。 Can I do this in on go, by reading through items in a list, or will it require several passes through the text file to clean up everything?我可以在旅途中通过阅读列表中的项目来完成此操作,还是需要多次通过文本文件来清理所有内容? TIA. TIA。

It's easy.这很简单。 Check my code below :在下面检查我的代码:

filePath = "your old file path"
newFilePath = "your new file path"

# we are going to list down which lines start with "#" or just blank
marker = []

with open(filePath, "r") as file:
    content = file.readlines() # read all lines and store them into list

for i in range(len(content)): # loop into the list
    if content[i][0] == "#" or content[i] == "\n": # check if the line starts with "#" or just blank
        marker.append(i) # store the index into marker list

with open(newFilePath, "a") as file:
    for i in range(len(content)): # loop into the list
        if not i in marker: # if the index is not in marker list, then continue writing into file
            file.writelines(content[i]) # writing lines into file

The point is, we need to read all the lines first.关键是,我们需要先阅读所有的行。 And check line by line whether it starts with # or it's just blank.并逐行检查它是否以#开头或只是空白。 If yes, then store it into a list variable.如果是,则将其存储到列表变量中。 After that, we can continue writing into new file by checking if the index of the line is in marker or not.之后,我们可以通过检查该行的索引是否在标记中来继续写入新文件。

Let me know if you have problem.如果您有问题,请告诉我。

How about using the ternary operator?如何使用三元运算符?

 #First option: within your for loop
 line = "" if "#" in line or not line else line

 #Second option: with list comprehension
 newFile = ["" if not line or "#" in line else line for line in oldfile]

I'm not sure if the ternary would work because if the string is empty, an Exception should be shown because "#" won't be in an empty string... How about我不确定三元是否有效,因为如果字符串为空,则应显示异常,因为“#”不会在空字符串中......怎么样

#Third option: "Staging your conditions" within your for loop
#First, make sure the string is not empty
if line:
    #If it has the "#" char in it, delete it
    if "#" in line:
        line = ""
#If it is, delete it
else: 
    line = ""

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

相关问题 我必须删除文本文件中的特定行 - I have to delete specific lines in text file 删除 .txt 文件中的特殊字符、特定文本和空行 - Delete special character, specific texts and blank lines in .txt file 如何从文件中删除行直到特定字符? [Python] - How to delete lines from a file until a specific character? [Python] 尝试根据关键字从文件中删除特定行 - Trying to delete specific lines from file based on keyword 如何在文本文件中搜索包含特定单词的行,然后使用“找到”行创建一个新文件 - How to search a text file for lines that contain a specific word, then create a new file with the "found" lines 在python中,如何删除不包含特定单词的表格文本格式的行? - In python, how can you delete lines in a tabular text format that do NOT contain a specific word? 如何从 python 中的文本文件中删除特定行 - How to delete specific lines from a text file in python 如何使用 Python 从文本文件中自动删除特定字符? - How to delete a specific character automatically from a text file using Python? 如何从文本文件中删除所有包含小于给定值的数字的行? - How to delete all lines that contain a number lower than given value from a text file? 删除不包含特定文本的行 - Delete rows that do not contain specific text
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM