如何在文本文件中写一行

编程入门 行业动态 更新时间:2024-10-12 10:17:55
本文介绍了如何在文本文件中写一行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

嘿那里, 有点新手问题。 i知道如何读取txt文件的行。 i知道怎么写一个txt文件。 但我如何用另一个值覆盖一个行值呢? i意思是,怎么去,比方说,行3.文本文件中的3个并替换 第3行写的东西是什么? 谢谢 <> <

解决方案

ne *** **@xit 写道:

嘿那里,有点新手问题。我知道如何阅读txt文件的行。 但是如何用另一个值覆盖一个行值? 我的意思是,怎么去,比方说,行3文本文件并替换第3行写的是什么? 感谢<><

你不应该尝试更新放置"除非你的文件有固定长度的行或记录。改变现有文件的长度 不能轻易进行,即使在可能的情况下也可能不会进行。 解决此问题的经典方法是将文件作为输入读取, 处理其内容并编写新文件,最终将 替换为原始文件一个相当复杂的重命名和删除之舞。 在Python中你可以使用文本文件的readlines()方法来构建一个列表 of all文件中的行。这样可以很容易地更改编号的 行。修改了内存中文件的内容后,您可以使用新文件的writelines()方法创建新文件。诀窍是 避免在发生低概率碰撞时丢失旧文件和新文件 . 自你是newbile(?)我会建议反对偏执狂 - 写下你的 代码而不用担心错误处理。你会很高兴地知道当你开始对错误处理Python感兴趣时,你会发现你需要的一切。 br $> 问候 Steve - Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb/

食谱是 *打开你的文件阅读:f = open(''filename.txt'','' r'') *读取列表中的所有行:content = f.readlines() *关闭文件:f.close() *将列表中的第三个元素设置为其他内容:content [2] = ''Blahdiblah'' *重新打开文件进行写入: f = open(''filename.txt'',''w'') *将列表写入文件:f.writelines(content) * close文件:f.close()

Gee高手,这么简单。 谢谢。从来没有想过只是在阅读时更改它然后 重写它。那会没事的。这些文件只有9行 长。 再次感谢!

Hey there, kinda newbie question here. i know how to read the lines of a txt file. i know how to write a txt file. but how do i overwrite a line value with another value ? i mean, how do go to, say, line 3 of a text file and replace what is written on line 3 with something else? thanks <><

解决方案

ne*****@xit wrote:

Hey there, kinda newbie question here. i know how to read the lines of a txt file. i know how to write a txt file. but how do i overwrite a line value with another value ? i mean, how do go to, say, line 3 of a text file and replace what is written on line 3 with something else? thanks <><

You shouldn''t try do "update in place" like that unless your file has fixed-length lines or records. Altering the length of an existing file isn''t to be lightly undertaken, and even when possible will likely not be undertaken. The classic way to approach this problem is to read the file as input, processing its contents and writing a new file, which eventually replaces the original in a fairly complex dance of renaming and deletion. In Python you can use a text file''s readlines() method to build a list of all the lines in a file. That makes it quite easy to change numbered lines. Having modified the file''s content in memory you can then create a new file using the writelines() method of a new file. The trick is to avoid losing both the old and the new files when a low-probability crash occurs. Since you are newbile (?) I would advise against paranoia - write your code without worrying about error handling. You''ll be pleased to know that when you start to take a serious interest in error handling Python has everything you''ll need. regards Steve -- Steve Holden +44 150 684 7255 +1 800 494 3119 Holden Web LLC www.holdenweb/

A recipe is * open your file for reading: f = open(''filename.txt'', ''r'') * read all lines in a list: content = f.readlines() * close the file: f.close() * set the third element in the list to something else: content[2] = ''Blahdiblah'' * re-open the file for writing: f = open(''filename.txt'', ''w'') * write the list to the file: f.writelines(content) * close the file: f.close()

Gee whiz, so easy. thanks. Never thought about just changing it while it was read then re-writing it. that will be just fine. these files are only 9 lines long. thanks again !

更多推荐

如何在文本文件中写一行

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

发布评论

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

>www.elefans.com

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