为什么不打印我的信息python csv?

时间:2013-07-19 02:59:56

标签: python csv

我觉得我的代码有问题,但命令提示符并没有说错误。

我是否将代码写错了?我希望它从我放的网址中获取数据(所以我写好了代码)并在旧数据下打印新数据而不创建新标题(因为我将每小时运行一次)。

这是我想要的结果。

result

这里是我的代码:

#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

def get_information(url):
  try:
    wunder_url_obj = urllib2.urlopen('http://api.wunderground.com/api/8d3b5d3fa03ddb6f/conditions/weather/q/China/Beijing.json')
  except:
    print 'Could not open URL'
    return None

  else:
    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 = wunder_url_obj.read()
    parsed_json = json.loads(json_string)
    temp_f = parsed_json['current_observation']['temp_f']
    weather = parsed_json['current_observation']['weather']
    date = str(now.month) + "/" + str(now.day) +  "/" + str(now.year) + " " +     str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)
    return ','.join([date, weather, str(temp_f)]) + '\n'
    now = datetime.now()    
    header = "Datetime,current condition,Temperature,\n" 

    f = open('out.csv','a')
    prev_data = open('out.csv', 'r').read()



# Add a header only if the file is empty
  if prev_data == '':
    f.write(header)

  f.write(','.join([date,str(temp_f),weather]))
  f.write('\n')
  f.close()

并在我的编辑器中:

enter image description here

1 个答案:

答案 0 :(得分:1)

看起来你似乎没有打电话给get_information

还有一个return ','.join([date, weather, str(temp_f)]) + '\n'可能会阻止预期的行为。

以下是修复:

#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

def get_information(url):
  try:
    wunder_url_obj = urllib2.urlopen(url)
  except:
    print 'Could not open URL'
    return None

  else:
    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 = wunder_url_obj.read()
    parsed_json = json.loads(json_string)
    temp_f = parsed_json['current_observation']['temp_f']
    weather = parsed_json['current_observation']['weather']
    date = str(now.month) + "/" + str(now.day) +  "/" + str(now.year) + " " +     str(now.hour) + ":" + str(now.minute) + ":" + str(now.second)
    now = datetime.now()    
    header = "Datetime,current condition,Temperature,\n" 

    f = open('out.csv','a')
    prev_data = open('out.csv', 'r').read()



  # Add a header only if the file is empty
  if prev_data == '':
    f.write(header)

  f.write(','.join([date,str(temp_f),weather]))
  f.write('\n')
  f.close()

get_information('http://api.wunderground.com/api/8d3b5d3fa03ddb6f/conditions/weather/q/China/Beijing.json')

结果:

bash$ cat out.csv 
Datetime,current condition,Temperature,
7/18/2013 23:14:6,82,Haze
7/18/2013 23:14:7,82,Haze