我有一个python脚本,它在一年中的每一天和每个月每分钟存储一行数据。但是,由于每天存储如此多的数据,json文件每年最终有518,400行数据,大小为67MB。
我需要做一个脚本,它执行一个行动,以创建一个新的json文件的每一个月的第一天,因此,它不会累积所有的数据在一个文件中,这将导致一个json文件为一月,二月,三月…十二月。
我的第一个想法是在python中使用if elif和else,但我不知道怎么做。
有人知道我是怎么做到的吗?
这是我的json文件的格式:i.imgur.com/jDzhWRI.png
发布于 2020-03-30 17:12:11
您可以执行以下操作:
从日期中提取年份和月份,并使用yyyy_mm后缀命名文件:
import datetime
dt = datetime.datetime.today()
filename = f"my_file_{dt.year}_{dt_month}"然后将您的数据写入指定的文件:
with open(filename,'a+') as f:
# the data will be appended to the file if it exists and if not, the
# file will be created
f.write(data)https://stackoverflow.com/questions/60880006
复制相似问题