Commit 51a9c42d authored by Børge Haugset's avatar Børge Haugset
Browse files

pre_forelesning

parent a342b9bc
# Ikke pensum!
import pandas as pd
import matplotlib.pyplot as plt
flix = pd.read_csv("netflix_titles.csv")
# Bare hiver ut en del ting, som director
flix.drop(["cast", "director"], axis=1, inplace=True)
# ...og der dato lagt til samt rating er null
flix = flix[flix["date_added"].isnull() == False]
flix = flix[flix["rating"].isnull()==False]
'''
Skrive til fil. Hele poenget her er at siden det finnes
elementer som inneholder komma, da kan ikke csv-filen
reparere på denne. Pandas som brukes ovenfor er flinkere.
Derfor må jeg importere filen med rådata, og lagre på
nytt med en annen separator (som jeg håper at ikke
roter til like mye. Men vi får se!)
'''
flix.to_csv("netflix_sane.csv", sep='#')
\ No newline at end of file
import pickle
liste = []
def read_netflix():
dict = {}
with open("netflix_sane.csv", 'r', encoding="utf-8") as f:
for line in f.readlines():
try:
liste.append(line.split("#"))
item = line.split("#")
dict[item[1]] = [item[3]] # title
dict[item[1]].append(item[2]) # type
dict[item[1]].append(item[4]) # country
dict[item[1]].append(item[6]) # year
dict[item[1]].append(item[-1]) # desc
except IndexError as e:
print('IndexError:', e, ":", item)
continue
return dict
def find_title(dict, name):
for k, v in dict.items():
if name in v[0]:
print(v[0])
def find_country(dict, country):
for k, v in dict.items():
try:
if country in v[2]:
print(f'{country}: {v[0]} ({v[3]}).')
except Exception as e:
print("Exception:", e, ":", v)
break
def save_dict(dict):
f = open('netflix.dat', 'wb')
pickle.dump(dict, f)
f.close()
def load_dict():
with open('netflix.dat', 'rb') as f:
return pickle.load(f)
dict = read_netflix()
find_title(dict, "Transform")
find_country(dict, "France")
\ No newline at end of file
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment