"""
Learning Goals:
 - Read and write data from files
 - Interpret data according to different protocols: plaintext, CSV, and JSON
 - Reformat data to find, add, remove, or reinterpret pre-existing data
"""

f = open("icecream.csv", "r")
print(f.read())
f.close()

###

f = open("tmp-15-110-dont-overwrite.txt", "w")
f.write("Hello world! Hopefully you didn't have a file with exactly this name!")
f.close()

###

import csv

f = open("icecream.csv", "r")
reader = csv.reader(f)
for row in reader:
    print(row)
f.close()

###

import json
f = open("icecream.json", "r")
data = json.load(f)
print(data)
f.close()

###

f = open("chat.txt", "r")
text = f.read()
f.close()

names = []
lines = text.split("\n")
for line in lines:
    if len(line.strip()) > 0:
        startIndex = line.find("From  ") + len("From  ")
        name = line[startIndex:]
    if "Privately" in name:
        endIndex = name.find("   to")
    else:
        endIndex = name.find(" :")
    name = name[:endIndex]
    names.append(name)

print(names)

###

f = open("icecream.csv", "r")
reader = csv.reader(f)
data = []
for row in reader:
    data.append(row)
f.close()

header = data[0]
data.pop(0)
for row in data:
    row.pop(0)
    chocCount = 0
    for i in range(len(row)):
        row[i] = row[i].lower()
        if "chocolate" in row[i]:
            chocCount += 1
    row.append(chocCount)

for row in data:
    if row[3] == 2:
        print(row)