import urllib.request
import csv

## CSV reading
file_name = 'budgie_budget.csv'

file = open(file_name)
# the csv module gives us a function reader() which takes an open file
reader = csv.reader(file)

for line in reader:
    #print(type(line))
    print(line)

# close the file, not the reader in this case
file.close()

## URL reading

url_reader = urllib.request.\
    urlopen('https://www.teach.cs.toronto.edu/~csc120h/fall/lectures/w9/file_demo_url.txt')

for line in url_reader:
    #print(type(line))
    print(line)
    
    # The output directly from the downloaded file
    # is not giving back strs.
    # It's giving back a collection of 'bytes'.
    
    # Every piece of information in your computer is stored in bits
    # a single bit can be 0 or 1.
    # A byte is 8 sequential bits: e.g., 00101001
    
    # Bytes are a very general form of information,
    # any data on your computer can be represented this way
    
    # We need to 'decode' these bytes to get it into our usual
    # string format
    
    print(line.decode())
    print(type(line.decode()))
    
# close the reader because it was used to open the file.
url_reader.close()

