In answering a question about how to get data out of Wunderground using Python I realised just how simple it is.

This snippet has all you need to collect daily data for sequential dates at a given weather station and write it to a CSV file. You can change the headers and code for building the rows to return whatever is available for the weather station you’re interested in. This one grabs a few days of data for Peking.

Jamie Bull | jamiebull1@gmail.com

Related Posts

WiGLE is a popular platform which can be used for finding the location of a device using the names of WiFi networks in its vicinity. I’ve written about this before, and wrote some Python code to interact with their API. This API has since been retired and replaced with a new one, as of December […]

Just a quick post to point out a couple of really useful tools.The first is a web-based tool for finding weather files for a location of interest. It’s similar to the Excel EPW finder tool we created a few years back, but much more modern looking. It is however missing a few of the useful […]

Eppy is a really useful library which I’ve written about several times, since before I really had anything to offer in terms of contributing code. Over the past year or so though, I’ve started to contribute back some of the changes and additions I’ve made while using eppy on academic and commercial projects.This post is […]

21 Comments on “Wunderground data to CSV”

  • e says:

    Which version of python is this for?

    thx

  • sb says:

    Hi, can be adapted the script to can export the data in 1 csv file for all the stations and to have the station_ids included on each day ?

  • sb says:

    Jamie,
    would you give me an ensign where should I start ? I would really appreciate your help.

    Thank you.

    • Jamie Bull says:

      Just think about how you would do it by hand, step by step. How do you want the data to be structured?

      The main thing is that you need to initialise your CSV file outside the main loop, so wove the with open... line above the for station_id in station_ids: line and fix the indentation.

      And then when you write each row you want to add the station ID. So after row = [] you want the next line to be row.append(station_id).

      That should be all you need to do.

      • sb says:

        same structure but after date to include the station, so in case we use multiple stations to know for what entry is each one.

        ultimately this format will be written into a table in db so to can plot.
        looking to look like bellow row.

        date | station_id | tempm | wspdm

        Thank you,
        sb

        • Jamie Bull says:

          Ok, so like I said above but with that row.append(station_id) being one line lower

          • sb says:

            Jamie,

            One more quick question: I do try to change for geolookup feature but getting error for geolokup in for location in data['geolookup']['location']: any sugestion what can be wrong ? Can you help me please?

  • Jamie Bull says:

    What error message are you getting? I notice you’ve removed the error handling.

  • DougIvey says:

    Hey Jamie,
    is it posible to set the start day ,to for example, to 2016.01.27 1am. Or can wunderground only work with days not with hours?
    I want to get the data from one day between hour x and hour y.
    Possible?

    Thanks for your help

    • Jamie Bull says:

      As far as I’m aware the API is only for entire days. It’s simple enough to grab that and then throw away what you don’t need though.

  • Karen says:

    Hi Jamie – Thanks for posting this. I wanted to provide an update which brings the code up to Python 3.5.2


    import csv
    import datetime
    import os

    import requests # this library makes html requests much simpler

    # add your API key (from wunderground) here
    api_key = "xxxxxxxxxxxxx"
    station_ids = ["PEK", ] # add more stations here if required

    for station_id in station_ids:
    print("Fetching data for station ID: %s" % station_id)
    try:
    # initialise your csv file
    with open('%s.csv' % station_id, 'w', newline = '') as outfile:
    writer = csv.writer(outfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
    headers = ['date','Dry-Bulb Temperature','Atmospheric Pressure','Dew-Point Temperature','Relative Humidity','wind speed','wind
    direction','pressure','Precip','Visibility','Solar Condition'] # edit these as required
    units = ['','C','hpa','C','%','m/s','Degrees','mm','km','']
    keys = ['tempm','pressurem','dewptm','hum','wspdm','wdird','precipm','vism','conds']
    writer.writerow(headers)
    writer.writerow(units)

    # enter the first and last day required here
    start_date = datetime.date(2017,1,1)
    end_date = datetime.date(2017,1,2)

    date = start_date
    while date <= end_date:
    # format the date as YYYYMMDD
    date_string = date.strftime('%Y%m%d')
    # build the url
    url = ("http://api.wunderground.com/api/%s/history_%s/q/%s.json" %
    (api_key, date_string, station_id))
    # make the request and parse json
    data = requests.get(url).json()
    # print(data)
    # build your row
    for history in data['history']['observations']:
    row = []
    row.append(str(history['date']['pretty']))
    for k in keys:
    row.append(str(history[k]))
    writer.writerow(row)
    # increment the day by one
    date += datetime.timedelta(days=1)
    except Exception:
    # tidy up
    os.remove(outfile)

    print("Done!")

    • Jamie Bull says:

      Hi Karen, Thanks for this but it seems to have lost formatting. You could try resubmitting with pre tags. Is the only change the brackets around print statements?

      • Karen says:

      • Karen says:

        print() is the only compatibility change that I can think of, but I also changed a few other things, such as adding more data points, and
        creating a list for the units and keys – that way all changes are made at the top of the file, not inside the loop lower down. Actually,
        headers[], units[] and keys[] should be moved up to line 6, as should start & end dates – this would be much cleaner

      • Karen says:

        …just remembered: print() isn’t the only change. There were a couple changes to the format in which the csv file is created.

        • Karen, thanks for the update! I was able to get your sample code to work with the “KBTV” station, but not my personal station
          “KORPORTL766”. Why would some stations work but not others?

Leave a Reply

Your email address will not be published. Required fields are marked *