Getting data from the web


In [1]:
## http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/BRV.csv

In [2]:
# Python request library lets us get data straight from a URL
import requests

In [4]:
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GBR.csv"
response = requests.get(url)

if response.status_code != 200:
    print ('Failed to get data: ', response.status_code)
else:
    print ('First 100 characters of data are: ')
    print (response.text[:100])


First 100 characters of data are: 
year,data
1901,8.16360092163086
1902,7.798388481140137
1903,8.02857780456543
1904,8.073899269104004

To do

* Get Temperatures from Guatemala

* Fetch rainfall for Afghanistan between 1980 and 1999


In [5]:
rl = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GTM.csv"
response = requests.get(url)

if response.status_code != 200:
    print ('Failed to get data: ', response.status_code)
else:
    print ('First 100 characters of data are: ')
    print (response.text [:100])


First 100 characters of data are: 
year,data
1901,8.16360092163086
1902,7.798388481140137
1903,8.02857780456543
1904,8.073899269104004


In [6]:
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/annualavg/pr/1980/1999/AFG.csv"
response = requests.get(url)

if response.status_code != 200:
    print ('Failed to get data: ', response.status_code)
else:
    print ('First 100 characters of data are: ')
    print (response.text [:100])


First 100 characters of data are: 
GCM,var,from_year,to_year,annual
bccr_bcm2_0,pr,1980,1999,429.92613067639326
cccma_cgcm3_1,pr,1980,1
# Create a csv file: Test01.csv 1901,12.3 1902,45.6 1903,78.9

In [7]:
with open ('Test01.csv','r') as reader:
    for line in reader:
        print (len(line))


10
10
9

In [8]:
with open ('Test01.csv', 'r') as reader:
    for line in reader:
        fields = line.split (',')
        print (fields)


['1901', '12.3\n']
['1902', '45.6\n']
['1903', '78.9']

In [9]:
# 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)


['1901', '12.3']
['1902', '45.6']
['1903', '78.9']

Using the csv library instead


In [10]:
import csv

In [11]:
with open ('Test01.csv', 'r') as rawdata:
    csvdata = csv.reader (rawdata)
    for record in csvdata:
        print (record)


['1901', '12.3']
['1902', '45.6']
['1903', '78.9']

In [12]:
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GBR.csv"
response = requests.get(url)

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:
        print (record)


['year', 'data']
['1901', '8.16360092163086']
['1902', '7.798388481140137']
['1903', '8.02857780456543']
['1904', '8.073899269104004']
['1905', '8.185888290405273']
['1906', '8.227397918701172']
['1907', '7.890880584716797']
['1908', '8.411910057067871']
['1909', '7.723073482513428']
['1910', '8.213129043579102']
['1911', '8.806525230407715']
['1912', '8.186320304870605']
['1913', '8.606131553649902']
['1914', '8.704795837402344']
['1915', '7.876689910888672']
['1916', '8.219968795776367']
['1917', '7.619339942932129']
['1918', '8.362971305847168']
['1919', '7.616194725036621']
['1920', '8.549764633178711']
['1921', '9.234434127807617']
['1922', '7.76489782333374']
['1923', '8.064897537231445']
['1924', '8.252241134643555']
['1925', '8.16501522064209']
['1926', '8.70051097869873']
['1927', '8.16069221496582']
['1928', '8.444143295288086']
['1929', '8.107586860656738']
['1930', '8.407665252685547']
['1931', '8.10656452178955']
['1932', '8.4717378616333']
['1933', '8.85051155090332']
['1934', '8.948348999023438']
['1935', '8.544142723083496']
['1936', '8.468003273010254']
['1937', '8.46855354309082']
['1938', '8.889307975769043']
['1939', '8.704442024230957']
['1940', '8.12307357788086']
['1941', '8.003026962280273']
['1942', '8.055621147155762']
['1943', '8.885298728942871']
['1944', '8.494339942932129']
['1945', '9.205503463745117']
['1946', '8.440408706665039']
['1947', '8.36552619934082']
['1948', '8.883687019348145']
['1949', '9.365959167480469']
['1950', '8.405385971069336']
['1951', '8.138639450073242']
['1952', '8.068081855773926']
['1953', '9.009355545043945']
['1954', '8.162971496582031']
['1955', '8.26194953918457']
['1956', '7.9771223068237305']
['1957', '8.857429504394531']
['1958', '8.321069717407227']
['1959', '9.36658763885498']
['1960', '8.690408706665039']
['1961', '8.73176097869873']
['1962', '7.695125579833984']
['1963', '7.550864219665527']
['1964', '8.439268112182617']
['1965', '7.835102081298828']
['1966', '8.296226501464844']
['1967', '8.56753158569336']
['1968', '8.260298728942871']
['1969', '8.16340446472168']
['1970', '8.346855163574219']
['1971', '8.870872497558594']
['1972', '8.271894454956055']
['1973', '8.508294105529785']
['1974', '8.534119606018066']
['1975', '8.876848220825195']
['1976', '8.866470336914062']
['1977', '8.389740943908691']
['1978', '8.330581665039062']
['1979', '7.717060089111328']
['1980', '8.417374610900879']
['1981', '8.192609786987305']
['1982', '8.705266952514648']
['1983', '8.807822227478027']
['1984', '8.649764060974121']
['1985', '7.863011360168457']
['1986', '7.717177391052246']
['1987', '8.07802677154541']
['1988', '8.803970336914062']
['1989', '9.231642723083496']
['1990', '9.37519645690918']
['1991', '8.681564331054688']
['1992', '8.806879043579102']
['1993', '8.423270225524902']
['1994', '8.923505783081055']
['1995', '9.22822380065918']
['1996', '8.369221687316895']
['1997', '9.520047187805176']
['1998', '9.21104621887207']
['1999', '9.363954544067383']
['2000', '9.130817413330078']
['2001', '8.875746726989746']
['2002', '9.512499809265137']
['2003', '9.573152542114258']
['2004', '9.492963790893555']
['2005', '9.423820495605469']
['2006', '9.706446647644043']
['2007', '9.62161922454834']
['2008', '9.148781776428223']
['2009', '9.309747695922852']
['2010', '8.206398963928223']
['2011', '9.608891487121582']
['2012', '8.891592025756836']

In [13]:
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/tas/year/GBR.csv"
response = requests.get(url)

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)


1901 8.16360092163086
1902 7.798388481140137
1903 8.02857780456543
1904 8.073899269104004
1905 8.185888290405273
1906 8.227397918701172
1907 7.890880584716797
1908 8.411910057067871
1909 7.723073482513428
1910 8.213129043579102
1911 8.806525230407715
1912 8.186320304870605
1913 8.606131553649902
1914 8.704795837402344
1915 7.876689910888672
1916 8.219968795776367
1917 7.619339942932129
1918 8.362971305847168
1919 7.616194725036621
1920 8.549764633178711
1921 9.234434127807617
1922 7.76489782333374
1923 8.064897537231445
1924 8.252241134643555
1925 8.16501522064209
1926 8.70051097869873
1927 8.16069221496582
1928 8.444143295288086
1929 8.107586860656738
1930 8.407665252685547
1931 8.10656452178955
1932 8.4717378616333
1933 8.85051155090332
1934 8.948348999023438
1935 8.544142723083496
1936 8.468003273010254
1937 8.46855354309082
1938 8.889307975769043
1939 8.704442024230957
1940 8.12307357788086
1941 8.003026962280273
1942 8.055621147155762
1943 8.885298728942871
1944 8.494339942932129
1945 9.205503463745117
1946 8.440408706665039
1947 8.36552619934082
1948 8.883687019348145
1949 9.365959167480469
1950 8.405385971069336
1951 8.138639450073242
1952 8.068081855773926
1953 9.009355545043945
1954 8.162971496582031
1955 8.26194953918457
1956 7.9771223068237305
1957 8.857429504394531
1958 8.321069717407227
1959 9.36658763885498
1960 8.690408706665039
1961 8.73176097869873
1962 7.695125579833984
1963 7.550864219665527
1964 8.439268112182617
1965 7.835102081298828
1966 8.296226501464844
1967 8.56753158569336
1968 8.260298728942871
1969 8.16340446472168
1970 8.346855163574219
1971 8.870872497558594
1972 8.271894454956055
1973 8.508294105529785
1974 8.534119606018066
1975 8.876848220825195
1976 8.866470336914062
1977 8.389740943908691
1978 8.330581665039062
1979 7.717060089111328
1980 8.417374610900879
1981 8.192609786987305
1982 8.705266952514648
1983 8.807822227478027
1984 8.649764060974121
1985 7.863011360168457
1986 7.717177391052246
1987 8.07802677154541
1988 8.803970336914062
1989 9.231642723083496
1990 9.37519645690918
1991 8.681564331054688
1992 8.806879043579102
1993 8.423270225524902
1994 8.923505783081055
1995 9.22822380065918
1996 8.369221687316895
1997 9.520047187805176
1998 9.21104621887207
1999 9.363954544067383
2000 9.130817413330078
2001 8.875746726989746
2002 9.512499809265137
2003 9.573152542114258
2004 9.492963790893555
2005 9.423820495605469
2006 9.706446647644043
2007 9.62161922454834
2008 9.148781776428223
2009 9.309747695922852
2010 8.206398963928223
2011 9.608891487121582
2012 8.891592025756836