gpt4 book ai didi

python - 在不加载文件的情况下将文本添加到行尾

转载 作者:太空狗 更新时间:2023-10-30 01:01:18 25 4
gpt4 key购买 nike

我需要以许多字典的形式将信息存储到一个非常大的文件中。这并不重要,只是说我试图先将所有数据放入这些词典中,然后我用完了内存 (~60Gb)。

很好,所以我想在文件中添加数据而不是将其实际加载到内存中,方法是在行上循环并在每一行上附加一些文本。那可能吗?如果是,怎么办?

最佳答案

您是否自己尝试过任何代码,您的发现是什么?您可能会采用以下方法:

with open('/tmp/bigfile.new', 'w') as output:
with open('/tmp/bigfile', 'r') as input:
while True:
line = input.readline().strip()
if not line:
break
line += ' Look ma, no hands!'
print(line, file=output)

当然,除非你有额外的字典,而不是'look ma no hands';-)

关于python - 在不加载文件的情况下将文本添加到行尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27507685/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com