var is either tas or pr ext is usually CSV iso3 is the ISO standard 3 letter code for the country of interest (capitals) look up country codes online
In [2]:
# Python request library lets us get data straight from a URL
import requests
In [9]:
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GBR.csv"
response = requests.get(url ) # gets the get function from the request library to find the url and put it in a loop etc
if response.status_code != 200:
print ('Failed to get data: ', response.status_code)
else:
print ('First 100 charecters of data are: ')
print (response.text[:100])
In [8]:
url = 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/GTM.csv'
response = requests.get(url ) # gets the get function from the request library to find the url and put it in a loop etc
if response.status_code != 200:
print ('Failed to get data: ', response.status_code)
else:
print ('First 100 charecters of data are: ')
print (response.text[:100])
In [13]:
url = 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/1980/1999/AFG.csv'
response = requests.get(url ) # gets the get function from the request library to find the url and put it in a loop etc
if response.status_code != 200:
print ('Failed to get data: ', response.status_code)
else:
print ('First 100 charecters of data are: ')
print (response.text[:100])
In [14]:
# Create a csv file: test01.csv
1901, 12.3
1902, 45.6
1903, 78.9
In [15]:
with open('test01.csv', 'r') as reader:
for line in reader:
print (len(line))
In [17]:
with open ('test01.csv', 'r') as reader:
for line in reader:
fields = line.split (',')
print (fields)
In [18]:
# We need to get rid of the hidden newline \n
with open ('test01.csv', 'r') as reader:
for line in reader:
fields = line.strip().split (',')
print (fields)
In [19]:
import csv
In [23]:
with open ('test01.csv', 'r') as rawdata:
csvdata = csv.reader(rawdata)
for record in csvdata:
print (record)
In [31]:
url = 'http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GTM.csv'
response = requests.get(url ) # gets the get function from the request library to find the url and put it in a loop etc
if response.status_code != 200:
print ('Failed to get data: ', response.status_code)
else:
wrapper = csv.reader(response.text.strip().split('\n'))
for record in wrapper:
if record[0] != 'year' :
year = int(record[0])
value = float(record[1])
print (year, value)
In [ ]: