1

我正在制作一个从网站获取数据然后将其记录在文本文件中的 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

谢谢!

4

1 回答 1

0

使用双端队列,因为它提供了 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 的技巧(使用由文件描述符制成的迭代器创建双端队列)并允许自己使用不同的源文件和目标文件,它可以变得非常短:

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)
于 2013-10-09T23:24:34.887 回答