如何在txt文件中插入一些单词(How to insert some words in the txt file)

编程入门 行业动态 更新时间:2024-10-24 09:30:56
如何在txt文件中插入一些单词(How to insert some words in the txt file)

现在我想在文件中添加一些单词,但是我只能通过使用f.open('xxx.txt','a')在文件末尾添加一些单词(.txt)。如果我使用'a +'模式,这句话将从头开始发生。 我希望在第一行插入一些句子而不用改变原来的句子。

# using python 2.7.12 f = open('Bob.txt','w') f.write('How many roads must a man walk down' ' \nBefore they call him a man' ' \nHow many seas must a white dove sail' ' \nBefore she sleeps in the sand' ' \nHow many times must the cannon balls fly' " \nBefore they're forever banned" " \nThe answer my friend is blowing in the wind" " \nThe answer is blowing in the wind") f.close() # add spice to the song f1 = open('Bob.txt','a') f1.write("\n1962 by Warner Bros.inc.") f1.close() # print song f2 = open('Bob.txt','r') a = f2.read() print a

我希望在第一行插入“Bob Dylan”,我应该添加什么代码?

Now I want to add some words in a file, but I could only add some words in the end of file(.txt) via using f.open('xxx.txt','a') If I use 'a+' mode, the words will take place the old one from beginning. I hope to insert some sentences in the first line without change the original ones.

# using python 2.7.12 f = open('Bob.txt','w') f.write('How many roads must a man walk down' ' \nBefore they call him a man' ' \nHow many seas must a white dove sail' ' \nBefore she sleeps in the sand' ' \nHow many times must the cannon balls fly' " \nBefore they're forever banned" " \nThe answer my friend is blowing in the wind" " \nThe answer is blowing in the wind") f.close() # add spice to the song f1 = open('Bob.txt','a') f1.write("\n1962 by Warner Bros.inc.") f1.close() # print song f2 = open('Bob.txt','r') a = f2.read() print a

I hope to insert " Bob Dylan" in the first line, What code shall I add ?

最满意答案

你不能那样做。 你应该阅读这个文件,修改它并重写它:

with open('./Bob.txt', 'w') as f : f.write('How many roads must a man walk down' ' \nBefore they call him a man' ' \nHow many seas must a white dove sail' ' \nBefore she sleeps in the sand' ' \nHow many times must the cannon balls fly' " \nBefore they're forever banned" " \nThe answer my friend is blowing in the wind" " \nThe answer is blowing in the wind") # add spice to the song file = [] with open('Bob.txt', 'r') as read_the_whole_thing_first: for line in read_the_whole_thing_first : file.append(line) file = ["1962 by Warner Bros.inc.\n"] + file with open('Bob.txt', 'r+') as f: for line in file: f.writelines(line) with open('Bob.txt', 'r') as f2: a = f2.read() print a

结果:

1962 by Warner Bros.inc. How many roads must a man walk down Before they call him a man How many seas must a white dove sail Before she sleeps in the sand How many times must the cannon balls fly Before they're forever banned The answer my friend is blowing in the wind The answer is blowing in the wind

You can't do that. You should read the file, modify it and rewrite it :

with open('./Bob.txt', 'w') as f : f.write('How many roads must a man walk down' ' \nBefore they call him a man' ' \nHow many seas must a white dove sail' ' \nBefore she sleeps in the sand' ' \nHow many times must the cannon balls fly' " \nBefore they're forever banned" " \nThe answer my friend is blowing in the wind" " \nThe answer is blowing in the wind") # add spice to the song file = [] with open('Bob.txt', 'r') as read_the_whole_thing_first: for line in read_the_whole_thing_first : file.append(line) file = ["1962 by Warner Bros.inc.\n"] + file with open('Bob.txt', 'r+') as f: for line in file: f.writelines(line) with open('Bob.txt', 'r') as f2: a = f2.read() print a

result :

1962 by Warner Bros.inc. How many roads must a man walk down Before they call him a man How many seas must a white dove sail Before she sleeps in the sand How many times must the cannon balls fly Before they're forever banned The answer my friend is blowing in the wind The answer is blowing in the wind

更多推荐

本文发布于:2023-08-06 18:19:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1453856.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单词   文件   如何在   txt   file

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!