如何写入文本文件中每一行的开头和结尾?
我目前正在寻找一种方法来写入Python 中文本文件每一行的开头和结尾。
例如,
当前 TXT 文档:
Jimmy
Was
Here
将第 1 个值写入每行的开头
111Jimmy
111Was
111Here
将第 2 个值写入每行的末尾
111Jimmy222
111Was222
111Here222
可以'谷歌上似乎找不到任何描述如何正确完成此操作的内容。我找到了写入特定行的方法,但并非所有方法都以这种方式进行。
I'm currently looking for a way to write to the beginning and end of every line of a text file in Python.
For example,
Current TXT document:
Jimmy
Was
Here
Write the 1st VALUE to the beginning of every line
111Jimmy
111Was
111Here
Write the 2nd VALUE to the end of every line
111Jimmy222
111Was222
111Here222
Can't seem to find anything on Google that describes how to properly have this done. I've found methods of writing to specific lines, but not all of them in this way.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
fileinput
与inplace=1
:You can make changes to the file without opening multiple files by using
fileinput
withinplace=1
:假设您已经使用列表中的 readlines() 读取了文件,您可以执行以下操作
,然后将数据写回文件......
Assuming you have read the file using readlines() in a list you can do
and then write data back to the file....