简体   繁体   English

如何从流附加到文件而不是在Python中覆盖

[英]How to append to a file from stream rather than overwrite in Python

I am new to python so this maybe a simple question but I have a kafka consumer from which I read in messages. 我是python的新手,所以这可能是一个简单的问题,但是我有一个kafka使用者,可以从中读取消息。 Every time a new message comes in it rewrites the previous message into the order.json file however I want to append it instead. 每次收到新消息时,它将以前的消息重写为order.json文件,但是我想附加它。 Additionally, I want to make sure the messages do not get read in no faster than every 1 second using possible some sort of pause. 另外,我想确保使用某种可能的暂停不会以不超过每1秒的速度读取消息。 Any tips on how to do this would be much appreciated. 有关如何执行此操作的任何提示将不胜感激。 Here is my current code 这是我当前的代码

for message in consumer:
     with open('order.json', 'w') as file:
          file.write(message.value.decode('UTF-8'))

You want to open your file in append mode . 您想以追加模式打开文件。 Also, you may not want to open the file on each message, since it can be an expensive operation (eg you will need to change file metadata like modification time every time you close the file): 另外,您可能不想在每条消息上打开文件,因为这可能是一项昂贵的操作(例如,每次关闭文件时,您都需要更改文件元数据,例如修改时间):

# open file in append mode, once
with open('order.json', 'a') as file:
    for message in consumer:
        file.write(message.value.decode('UTF-8'))

As for rate limiting, you could start with something simple, like the following: 至于速率限制,您可以从简单的内容开始,如下所示:

import time

def ratelimit(it, sleep=1.0):
    for v in it:
        yield v
        time.sleep(sleep)

if __name__ == '__main__':
    for i in ratelimit(range(10)):
        print(i)

This would ensure, there is at least one second delay between successive values from an iterator. 这将确保来自迭代器的连续值之间存在至少一秒钟的延迟。 Here's a asciicast showing the rate-limiter in action. 这是一个显示了速率限制器作用的画作

Open the file in append mode as ' w' (write mode) truncates the file each time it exists append模式打开文件,因为“ w' (写入模式)每次存在时都会截断文件

for message in consumer:
    with open('order.json', 'a') as file:
        file.write(message.value.decode('UTF-8'))

Open outside the loop: 在循环外打开:

with open('order.json', 'w') as file:
    for message in consumer:         
          file.write(message.value.decode('UTF-8'))

Or open outside and use a if you are going to be appending for each run: 或者打开之外,使用a ,如果你将要追加每次运行:

with open('order.json', 'a') as file:
    for message in consumer:         
          file.write(message.value.decode('UTF-8'))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM