Python:从文本文件中splitlines()然后将它们写回文本文件

时间:2013-10-09 22:50:03

标签: python

我正在制作一个从网站获取数据然后将其记录在文本文件中的python程序。我希望这能记录最后1000个(我正在测试4和一个字符串“hello”)条目并删除其余部分。以下是我到目前为止的情况:

f = open("test.txt", "r")
text = f.read()

f = open("test.txt", "w")
content = text.splitlines(True)
f.write("hello")
f.write("\n")

for x in range(0,4):
    f.write(str(content[x:x+1]).strip('[]'))

f.close()

然而,“工作”会将文本文件格式化为:

hello
'hello\n''\'hello\\n\'\'\\\'hello\\\\n\\\'\\\'\\\\\\\'hello\\\\\\\\n\\\\\\\'"\\\\\\\'hello\\\\\\\\\\\\\\\\n\\\\\\\'"\\\'\''

你能帮我解决这个问题,看起来像这样:

hello
hello
hello
hello

谢谢!

1 个答案:

答案 0 :(得分:0)

使用deque,因为它提供了maxlen。添加行/单词将只保留maxlen项目,新项目将被添加,旧项目将被遗忘。

from collections import deque
fname = "source.txt"
last_lines = deque(maxlen = 4)
with open(fname) as f:
  text = f.read()
  for line in text.splitlines(True):
    last_lines.append(line)
#f is closed when we leave the block 

outfname = fname
with open(outfname, "w") as of:
  for line in last_lines:
    of.write(line)

即使没有分割线也可以这样做(但你要求它)。

from collections import deque
fname = "source.txt"
last_lines = deque(maxlen = 4)
for line in open(fname):
  last_lines.append(line)
#file is closed when we leave the (for) block

outfname = fname
with open(outfname, "w") as of:
  for line in last_lines:
    of.write(line)

使用Jon Clements的技巧(使用文件描述符创建的迭代器创建deque)并允许自己使用不同的源文件和目标文件,它可以变得非常简短:

from collections import deque
with open("target.txt", "w") as out_f:
  for line in deque(open("source.txt"), maxlen = 4):
    out_f.write(line)