写到某一行? [英] Writing to a certain line?

查看:74
本文介绍了写到某一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有办法获取txt文件,而

保留大部分文件,只替换一行。看,我会有一个类似的文件:


Tommy 555

Bob 62

Joe 529


我想把它设置为:


Tommy 555

Bob 66

Joe 529


有没有简单的方法呢?

I was wondering if there was a way to take a txt file and, while
keeping most of it, replace only one line. See, I''d have a file like:

Tommy 555
Bob 62
Joe 529

And I''d want to set it to be:

Tommy 555
Bob 66
Joe 529

Is there any easy way to do this?

推荐答案

Tommy B:
我想知道是否有办法获取txt文件,而且保留大部分文件,只替换一行。
I was wondering if there was a way to take a txt file and, while
keeping most of it, replace only one line.




你需要阅读文件并解析它,找到你想要改变的行的起始位置

。然后向该位置寻求输出并写入

并刷新更改。您必须在此

操作期间保持文件锁定,以防止其他进程在这些步骤之间更改文件

。你只能用长度相同的字符串替换字符串。
http://docs.python.org/lib/bltin-file-objects.html


-
$ b $bRenéPijlman



You''d need to read the file and parse it, to find the start position of
the line you want to change. Then seek output to that position and write
and flush the changes. You must keep the file locked during this
operation, to prevent other processes from changing the file in between
these steps. You can only replace a string with a string of the same
length.
http://docs.python.org/lib/bltin-file-objects.html

--
René Pijlman


Tommy B写道:
Tommy B wrote:
我想知道是否有办法获取txt文件和保留大部分内容时,只更换一行。
I was wondering if there was a way to take a txt file and, while
keeping most of it, replace only one line.




< meta>

这是常见问题解答(同时我不知道它是否在常见问题解答中! - ),并且没有像Python问题那样的b $ b方式。 FWIW,这也是CS101 ...

< / meta>


你不能用文本文件(这将是可以使用

固定长度的二进制格式。


这样做的规范方法 - 无论用什么语言,都是写

在新文件中修改了版本,然后替换旧文件。


import os

old = open(" / path / to / file .txt"," r")

new = open(" /path/to/new.txt"," w")

for old in old :

如果line.strip()==" Bob 62"

line = line.replace(" 62"," 66")

new.write(line)

old.close()

new.close()

os.rename(" /path/to/new.txt" ;," /path/to/file.txt")


如果你经常做这种操作而不在乎关于

文件是人类可读的,你可能会更好地使用一些轻量级的

数据库系统m(berkeley,sqlite,...)或任何其他现有的lib''

来处理血腥细节。


否则 - 如果你想/需要坚持使用人类可读的平面文本文件 -

至少写一个可靠的librairy来处理这个,这样你就可以保留客户代码

免费的技术文件。


HTH

-

bruno desthuilliers

python -c" print''@''。加入([''。''。join([w [:: - 1] for p in p.split(''。'')])for

p in''o *** *@xiludom.gro''。split(''@'')])"



<meta>
This is a FAQ (while I don''t know if it''s in the FAQ !-), and is in no
way a Python problem. FWIW, this is also CS101...
</meta>

You can''t do this in place with a text file (would be possible with a
fixed-length binary format).

The canonical way to do so - whatever the language, is to write the
modified version in a new file, then replace the old one.

import os
old = open("/path/to/file.txt", "r")
new = open("/path/to/new.txt", "w")
for line in old:
if line.strip() == "Bob 62"
line = line.replace("62", "66")
new.write(line)
old.close()
new.close()
os.rename("/path/to/new.txt", "/path/to/file.txt")

If you have to do this kind of operation frequently and don''t care about
files being human-readable, you may be better using some lightweight
database system (berkeley, sqlite,...) or any other existing lib that''ll
take care of gory details.

Else - if you want/need to stick to human readable flat text files - at
least write a solid librairy handling this, so you can keep client code
free of technical cruft.

HTH
--
bruno desthuilliers
python -c "print ''@''.join([''.''.join([w[::-1] for w in p.split(''.'')]) for
p in ''o****@xiludom.gro''.split(''@'')])"


bruno at modulix写道:
bruno at modulix wrote:
< meta>
这是一个常见问题解答(虽然我不知道它是否在常见问题解答中! - ),并且不是一个Python问题。 FWIW,这也是CS101 ...
< / meta>
<meta>
This is a FAQ (while I don''t know if it''s in the FAQ !-), and is in no
way a Python problem. FWIW, this is also CS101...
</meta>




可以在这里重新发布您的回复:

http://pyfaq.infogami.com/suggest

< / F>



feel free to repost your response here:

http://pyfaq.infogami.com/suggest

</F>


这篇关于写到某一行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆