当脚本每小时运行一次时,我希望我的标题仅打印一次(我将为此使用窗口计划,因此不要专注于计划)以收集旧数据下的新数据。
我的实际打印:
我希望它是这样的:
这是我的代码(谁工作)
任何想法 ?
#Source : http://www.wunderground.com/weather/api/d/docs?d=resources/code-samples
import urllib2
import json
import time
import csv
from datetime import datetime#set the time
f = urllib2.urlopen('http://api.wunderground.com/api/8d3b5d3fa03ddb6f/conditions/weather/q/China/Beijing.json')
now = datetime.now()
current_year = now.year
current_day = now.day
current_month = now.month
current_hour = now.hour
current_minute = now.minute
current_second = now.second
json_string = f.read()
parsed_json = json.loads(json_string)
locations = parsed_json.get('locations', 'Beijing')
temp_f = parsed_json['current_observation']['temp_f']
weather = parsed_json['current_observation']['weather']
#--- Open the file + write on it ---
f = open('out.csv','a')
header = "Datetime,Location,Temperature,current_condition\n"
date = str(now.month) + "/" + str(now.day) + "/" + str(now.year) + " " + str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)
f.write(header)
f.write(','.join([date,locations,str(temp_f),weather]))
f.write('\n')
f.close()
# --- And Close the file ---