data_download.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. # 下载数据
  2. import sys
  3. from LIB.BACKEND import DBManager
  4. from LIB.BACKEND.OPENAPI import OpenApi
  5. import datetime
  6. import os
  7. # 从开放平台获取资产列表及相关信息
  8. openApi = OpenApi.OpenApi()
  9. df_all_sn = openApi.get_asset()
  10. write_path = r'D:\Develop\Data\\'
  11. dbManager = DBManager.DBManager()
  12. start_time = '2021-11-10 00:00:00'
  13. end_time = '2021-12-13 00:00:00'
  14. st=datetime.datetime.strptime(start_time, '%Y-%m-%d %H:%M:%S')
  15. et = st + datetime.timedelta(days=1)
  16. while et <= datetime.datetime.strptime(end_time, '%Y-%m-%d %H:%M:%S'):
  17. time_dir = st.strftime('%Y-%m-%d %H:%M:%S')[0:10]
  18. if not os.path.exists(write_path+time_dir):
  19. os.mkdir(write_path+time_dir)
  20. filenames = os.listdir(write_path+time_dir)
  21. for sn in df_all_sn['sn'].tolist():
  22. if sn+".csv" in filenames:
  23. print("skip")
  24. continue
  25. df_data = dbManager.get_data(sn=sn, start_time=st.strftime('%Y-%m-%d %H:%M:%S'), end_time=et.strftime('%Y-%m-%d %H:%M:%S'), data_groups=['bms'])
  26. df_data['bms'].to_csv(write_path+time_dir+'\\'+sn+'.csv', index=False)
  27. st = et
  28. et = st + datetime.timedelta(days=1)
  29. print(sn+'下载完成')