使用python将文本文件中的一行替换为任何新行

使用python将文本文件中的一行替换为任何新行,python,file-handling,Python,File Handling,假设我有一个文本文件,名为details.txt details.txt 现在我有一个python文件,比如说update.py,我试图做的是用(id=1=missing)或(id=1=found)替换具有特定id的行,该id正在挂起(例如:id=1=pending)。 注意,我们只是将pending改为missing或find,rest id=1=是相同的。 例如,如果我们将details.txt中的id=1=pending替换为id=1=found,它应该是这样的: id=0=pendi

假设我有一个文本文件,名为details.txt
details.txt

现在我有一个python文件,比如说update.py,我试图做的是用(id=1=missing)或(id=1=found)替换具有特定id的行,该id正在挂起(例如:id=1=pending)。

注意,我们只是将pending改为missing或find,rest id=1=是相同的。 例如,如果我们将details.txt中的id=1=pending替换为id=1=found,它应该是这样的:

id=0=pending
id=1=found
id=abc2=pending
id=x2=found
#cursor always points to the new line since I am appending \n in the end of each line
id=0=pending
id=1=pending
id=x2=found
#cursor always points to the new line since I am appending \n in the end of each line

这是我写的
update.py

f = open("details.txt","r+")
temp = f.readlines()
for line in temp:
    word = line.split('=')#using = as a delimiter
    if word[1]=="1" and word[2]=="pending\n":    #pending\n because while writing, we are appending \n to each line
        striped = line.strip()
        new_line = striped.replace("pending","found")
        new_content = new_line     #id=1=found
        pos = f.tell()      #it will give the current position
        if pos-len(line)-1>=0:    #checking whether we have any line or not
            pos = pos-len(line)-1     #going to the start of a line and -1 to remove \n
        f.seek(pos)        #setting the pointer to the start of the line we want to replace , (start of 2nd line in this case)
        f.write(new_content)         #writing the content

f.close()


我得到的输出

id=0=pending
id=1=foundng
id=abc2=pending
id=x2=found
#cursor always points to the new line since I am appending \n in the end of each line


因此,我们得到的不是(id=1=found),而是(id=1=foundng),因为len(found)是5,len(pending)是7,所以在pending的末尾额外的2个字符,即ng不会被替换。

请告诉我该怎么做。
另外,请告诉我,如果我想删除某一行,那么我该如何做。
例如:
如果我想删除行(id=abc2=pending),我应该怎么做。
输出应该是这样的:

id=0=pending
id=1=found
id=abc2=pending
id=x2=found
#cursor always points to the new line since I am appending \n in the end of each line
id=0=pending
id=1=pending
id=x2=found
#cursor always points to the new line since I am appending \n in the end of each line

请注意,该行将与\n


任何帮助都将不胜感激

使用正则表达式将帮助您

由于您正在查找后跟单词
'pending'
的特定id,因此一个简单的正则表达式:
“id=your_id=pending”
将起作用

然后,您可以使用
re.sub
替换整行内容。这里
id=your\u id=missing/found“

注意:这个答案可能不是最好的,因为您重写了大多数行,但是对于小文件,它可以完美地工作


如果打印的是
new\u content
而不是
f.write(new content)
。可以看出,问题实际上不在replace函数中,单词会被替换

        # f.write(new_content)
        print(new_content)
输出:
id=1=按原样找到

问题是,当您覆盖预先存在的文件时,它会替换文件中已经存在的字符,在这种情况下,“待定”的最后两个字母不需要被覆盖

要解决此问题,您必须重写整个文件:

f = open("details.txt","r")
temp = f.readlines()

output_text = []
for line in temp:
    word = line.split('=')
    if word[1]=="1" and word[2]=="pending\n":
        line = line.replace("pending\n", "found\n") # replace pending with found 
    output_text.append(line)
f.close()

f = open("details.txt", "w")
f.write("".join(output_text))
f.close()
现在文件是:

id=0=pending
id=1=found
id=abc2=pending
id=x2=found

非常感谢你,它正是我想要的