如何通过Python在文件中删除某个行(给定的行#)?

发布于 2025-02-11 10:47:16 字数 614 浏览 2 评论 0原文

我想从一个.txt文件中删除一行文本,给定与TXT文件的行号相对应的整数。例如,给定整数2,删除文本文件的第2行。 我对投入计划的内容有些迷失。

f = open('text1.txt','r+')
g = open('text2.txt',"w")

line_num = 0
search_phrase = "Test"
for line in f.readlines():
    line_num += 1
    
    if line.find(search_phrase) >= 0:
        print("text found a line" , line_num)
        decision = input("enter letter corresponding to a decision: (d = delete lines, s = save to new txt) \n")
        if decision == 'd':
             //delete the current line
        if decision == 's':
             //save the current line to a new file

任何帮助都将受到赞赏!谢谢 :)

I want to delete a line of text from a .txt file given an integer corresponding to the txt file's line number. For example, given the integer 2, delete line 2 of the text file.
I'm sort of lost on what to put into my program.

f = open('text1.txt','r+')
g = open('text2.txt',"w")

line_num = 0
search_phrase = "Test"
for line in f.readlines():
    line_num += 1
    
    if line.find(search_phrase) >= 0:
        print("text found a line" , line_num)
        decision = input("enter letter corresponding to a decision: (d = delete lines, s = save to new txt) \n")
        if decision == 'd':
             //delete the current line
        if decision == 's':
             //save the current line to a new file

Any help is appreciated! Thanks :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

舟遥客 2025-02-18 10:47:16

这边走:

with open('text1.txt','r') as f, open('text2.txt',"w") as g:

    to_delete=[2,4]

    for line_number, line in enumerate(f.readlines(), 1):
            if line_number not in to_delete:
                g.write(line)
            else:
                print(f'line {line_number}, "{line.rstrip()}" deleted')

This way:

with open('text1.txt','r') as f, open('text2.txt',"w") as g:

    to_delete=[2,4]

    for line_number, line in enumerate(f.readlines(), 1):
            if line_number not in to_delete:
                g.write(line)
            else:
                print(f'line {line_number}, "{line.rstrip()}" deleted')
一页 2025-02-18 10:47:16

它去了。

f = open('data/test.txt','rb')
text = f.readlines() # all lines are read into a list and you can  acess it as a list

deleted_line = text.pop(1) #line is deleted and stored into the variable
print(text)
print(deleted_line)

f.write(text) # here you save it with the new data, you can always delete the data in the file to replace by the new one

Here it goes.

f = open('data/test.txt','rb')
text = f.readlines() # all lines are read into a list and you can  acess it as a list

deleted_line = text.pop(1) #line is deleted and stored into the variable
print(text)
print(deleted_line)

f.write(text) # here you save it with the new data, you can always delete the data in the file to replace by the new one
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文