Nardrek
Nardrek

Reputation: 477

Why is not printing my informations python csv?

I think something wrong in my code but the command prompt don't say there is an error.

Did i write the code wrong for open ? i want it take the data from the url i put ( so i wrote good the code) and print the new data under the old data without create a new header (because i will run it every hour).

Here is the result I want.

result

here my code :

#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()

and in my editor :

enter image description here

Upvotes: 0

Views: 143

Answers (1)

mgamba
mgamba

Reputation: 1199

It doesn't look like you ever call get_information.

There's also a return ','.join([date, weather, str(temp_f)]) + '\n' which might prevent intended behavior.

Here's the fix:

#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')

Result:

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

Upvotes: 1

Related Questions