<jupyter_start><jupyter_text>Getting Data from the the webThe world bank provides climate data via it's webhttp://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/var/year/iso3.extvar is either tas or pr ext is usually CSV iso3 is the ISO standard 3 letter code for the country of interset (in capitals)<jupyter_code># Python requets Library lets us get data straight from a URL
import requests
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 the data are:')
    print ( response.text[:100])<jupyter_output>First 100 characters of the data are:
year,data
1901,8.16360092163086
1902,7.798388481140137
1903,8.02857780456543
1904,8.073899269104004<jupyter_text>Gettting Status codes for Guatemala (Country Code is GTM)Fetch rainfall for Afghanistan between 1980 and 1999<jupyter_code>url = "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 the data are:')
    print ( response.text[:100])
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 the data are:')
    print ( response.text[:100])
# Create a csv file: test01.csv

1901,12.3
1902,45.6
1903,78.9
with open ('test01.csv', 'r') as reader:
    for line in reader:
        print (len (line))
with open ('test01.csv', 'r') as reader:
    for line in reader:
        fields = line.split(',')
        print (fields)
# 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)<jupyter_output>['1901', '12.3']
['1902', '45.6']
['1903', '78.9']<jupyter_text>Using CSV library instead<jupyter_code>import csv
with open ('test01.csv', 'r') as rawdata:
    csvdata = csv.reader(rawdata)
    for record in csvdata:
        print (record)
url = "http://climatedataapi.worldbank.org/climateweb/rest/v1/country/cru/pr/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)<jupyter_output>1901 89.73978424072266
1902 84.22315216064453
1903 119.31846618652344
1904 91.66348266601562
1905 89.65657043457031
1906 98.95526885986328
1907 98.48038482666016
1908 95.18717956542969
1909 98.14665985107422
1910 103.52928161621094
1911 95.45286560058594
1912 108.9598617553711
1913 93.2777099609375
1914 102.29579162597656
1915 97.06973266601562
1916 107.87296295166016
1917 98.88864135742188
1918 105.67728424072266
1919 98.01356506347656
1920 105.2422103881836
1921 86.1358871459961
1922 96.65888214111328
1923 113.46175384521484
1924 109.6202392578125
1925 98.27181243896484
1926 102.01596069335938
1927 110.8193359375
1928 115.2984619140625
1929 100.12940216064453
1930 108.5672607421875
1931 102.66221618652344
1932 101.97649383544922
1933 79.8314437866211
1934 100.34272766113281
1935 107.33514404296875
1936 98.53144836425781
1937 94.9148178100586
1938 111.8836441040039
1939 98.04186248779297
1940 97.34461975097656
1941 86.44178771972656
1942 99.07189178466797
1943 100.81599426269531
1944 [...]
{'fraction_non_alphanumeric': 0.10719957649550027, 'fraction_numerical': 0.28057173107464267, 'mean_word_length': 4.437410071942446, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '336827', 'license': 'MIT', 'n_tokens_mistral': 2069, 'n_tokens_neox': 1389, 'n_words': 348, 'path': '01-csv-data.ipynb', 'repo_name': 'alhamdubello/sc-python', 'size': '3778'}