继续解析由另一个进程更新的CSV文件 [英] Continuously parse CSV files which are updated by another process
问题描述
如果我有一堆 csv
文件,它们会定期更新。
让我们说csv文件是:
If I have a bunch of csv
files and they get updated periodically.
Let's say the csv files are:
file1.csv, file2.csv file3.csv
在更新过程中,数据附加到 csv
文件。
During the updating process, the data is appended to the last line of the csv
file.
可以从 csv
文件读取数据,并更新并存储在 array
或 collection(deque)
。
is it possible to read the data from the csv
file and as it updated and store it in a array
or collection(deque)
.
从csv文件中更新时收集数据?
Is there a way to collect the data from the csv file as it is updated?
推荐答案
您可以使用python包名为 Watchdog 。
You can use a python package called Watchdog.
此示例显示了对文件系统更改递归监视当前目录以及将任何日志记录到控制台: p>
This example shows monitoring the current directory recursively for file system changes, and logging any to the console:
import time
from watchdog.observers import Observer
from watchdog.events import LoggingEventHandler
if __name__ == "__main__":
event_handler = LoggingEventHandler()
observer = Observer()
observer.schedule(event_handler, path='.', recursive=True)
observer.start()
try:
while True:
time.sleep(1)
except KeyboardInterrupt:
observer.stop()
observer.join()
你可以结合使用Ignacio的回答 - use file_pointer.tell()
以获取文件中的当前位置,然后下一次 seek()
文件。例如:
You could use this in conjunction with Ignacio's answer - use file_pointer.tell()
to get the current position in the file, and then seek()
there next time, and read the remainder of the file. For example:
# First time
with open('current.csv', 'r') as f:
data = f.readlines()
last_pos = f.tell()
# Second time
with open('current.csv', 'r') as f:
f.seek(last_pos)
new_data = f.readlines()
last_pos = f.tell()
这篇关于继续解析由另一个进程更新的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!