import json
import csv
import datetime
import time
def judge(timestr):
if ":" not in timestr and "/" in timestr:
timestr=timestr.replace("/","-")
if ":" not in timestr and "-" in timestr:
timestr=timestr+" 0:0"
nowTime_str = datetime.datetime.now().strftime('%Y-%m-%d %H:%M')
e_time = time.mktime(time.strptime(nowTime_str,"%Y-%m-%d %H:%M"))
s_time = time.mktime(time.strptime(timestr, '%Y-%m-%d %H:%M'))
diff = abs(int(s_time)-int(e_time))
return -diff
import pandas as pd
import numpy as np
import numpy
name=['产地',
'品种',
'价格(元)',
'周环比(元)',
'价格类型',
'日期',
'年',
'月',
'日',
'最低温(℃)',
'最高温(℃)',
'天气',
'风向',
'级数',
'政策情感',
'调整日期',
'汽油价格(元/吨)',
'汽油涨跌',
'柴油价格(元/吨)',
'柴油涨跌',
'全国发电量(亿千瓦时)',
'水电(亿千瓦时)',
'火电(亿千瓦时)',
'核电(亿千瓦时)',
'风电(亿千瓦时)',
'全国全社会用电量(亿千瓦时)',
'全国全社会用电量同比增长(%)',
'第一产业用电量(亿千瓦时)',
'第二产业用电量(亿千瓦时)',
'第三产业用电量(亿千瓦时)',
'工业用电量(亿千瓦时)',
'城乡居民生活用电量(亿千瓦时)',
'全国发电装机容量(万千瓦)',
'全国供电煤耗率(克/千瓦时)',
'全国供热量(万百万千焦)',
'全国供热耗用原煤(万吨)',
'全国供电量(亿千瓦时)',
'全国发电设备累计平均利用小时',
'全国发电累计厂用电率(%)',
'电源工程投资完成(亿元)',
'电网工程投资完成(亿元)',
'新增发电装机容量(万千瓦)',
'新增220千伏及以上变电设备容量(万千伏安)',
'新增220千伏及以上输电线路长度(千米)']
with open('./data/防城港煤炭天气.csv', 'a', newline='', encoding='utf-8-sig') as fp:
writer = csv.writer(fp)
writer.writerow(name)
data1=pd.read_csv("防城港煤炭天气.csv",encoding='gbk')
data2=pd.read_csv("油价数据新表(1).csv",encoding='gbk')
for i in range(0,data1.index.stop):
time1=data1.iloc[data1.index.stop-i-1]['日期']
list1=list(data1.iloc[data1.index.stop-i-1])
for j in range(0,data2.index.stop):
time2=data2.iloc[data2.index.stop-j-1]['调整日期']
list2=list(data2.iloc[data2.index.stop-j-1])
time3=data2.iloc[data2.index.stop-j-2]['调整日期']
if judge(time1)>=judge(time2) and judge(time1)<judge(time3):
list1.extend(list2)
with open('./data/防城港煤炭天气.csv', 'a', newline='', encoding='utf-8-sig') as fp:
writer = csv.writer(fp)
writer.writerow(list1)
break