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

时间:2019-08-13 00:13:14

标签: python python-3.x

我正在测试下面的代码,但是它并没有执行我想要的操作。

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!!')

基本上,我想删除任何包含'#'字符的行(我要删除的行以'#'字符开头)。另外,我想删除完全空白的任何/所有行。我可以通过阅读列表中的项目来进行操作吗?还是需要多次遍历文本文件才能清理所有内容? TIA。

2 个答案:

答案 0 :(得分:1)

很简单。在下面检查我的代码:

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

重点是,我们需要首先阅读所有行。并逐行检查它是否以#开头还是空白。如果是,则将其存储到列表变量中。之后,我们可以通过检查行的索引是否在标记中来继续写入新文件。

让我知道您是否有问题。

答案 1 :(得分:0)

如何使用三元运算符?

 #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]

我不确定三元数是否可以工作,因为如果字符串为空,则应显示异常,因为“#”不会在空字符串中...怎么回事

#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 = ""