Python 为什么不打印我的信息?

Python 为什么不打印我的信息?,python,csv,Python,Csv,我认为我的代码有问题,但是命令提示符没有说有错误 我把open的代码写错了吗?我希望它从我放置的url中获取数据(因此我编写了很好的代码),并在旧数据下打印新数据,而不创建新标题(因为我将每小时运行一次) 这是我想要的结果 这是我的代码: #Source : http://www.wunderground.com/weather/api/d/docs?d=resources/code-samples import urllib2 import json import time import c

我认为我的代码有问题,但是命令提示符没有说有错误

我把open的代码写错了吗?我希望它从我放置的url中获取数据(因此我编写了很好的代码),并在旧数据下打印新数据,而不创建新标题(因为我将每小时运行一次)

这是我想要的结果

这是我的代码:

#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()
在我的编辑中:


看起来你从来没有打过电话来获取信息

还有一个
return','.join([date,weather,str(temp\u 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

您是否调用过
获取信息(url)
方法?我只是看到您定义了一个函数,而不是调用它。谢谢mgamba,所以问题是返回函数、url和我忘记调用的get information(u u)。如果我只想打开一次,只是为了提供信息(因为我们可以看到,我将两个open()。是否可以执行一对一的open()?为了保留我想要的结果?抱歉,我对python不熟悉。据了解,
a+
可能是您想要的。没问题!谢谢您的帮助:)非常感谢。