Mam do napisania funkcję, która ma wykonywać dokładnie to co jest w temacie. Posiadam plik items_test.csv, który podpinam do wiadomości. W pliku jest sporo linijek informacji, informacje w poszczególnej linii są oddzielone znakiem ";". Wszystko splitujemy i dzielimy na tablice. To rozumiem.
Mam taką funkcję służącą do otwierania pliku, podzielenia linii na tablice. W każdej lini najważniejszą dla mnie informacją jest czwarty element listy - słowo "in" albo "out". Nazwijmy to "kluczami", piąty element to ilość. In to dostawa. Out to powiedzmy sprzedaż.
Muszę obliczyć różnicę tych dwóch zmiennych. Chyba za pomocą pętli i instrukcji warunkowych. Ale nie wiem jak to zrobić
```def get_table_from_file(file_name):
"""
Reads csv file and returns it as a list of lists.
Lines are rows columns are separated by ";"
Args:
file_name (str): name of file to read
Returns:
list: List of lists read from a file.
"""
with open(file_name, "r") as file:
lines = file.readlines()
table = [element.replace("\n", "").split(";") for element in lines]
return table
Napisałem coś takiego, ale kompletnie to się kupy nie trzyma, nie wiem jak te linie informacji połączyć z funkcją zwracającą tablicę
def profit_in_out():
with open('items_test.csv', "r") as file:
for line in file.readlines():
line = line.split(";")
print(line)
for element in line:
if element[4] == "in":
print(element[5])
Moglibyście to napisać ? Chodzi o zrobienie takiej funkcji:
def which_year_max(table):
"""
Question: Which year has the highest profit? (profit = in - out)
Args:
table (list): data table to work on
Returns:
number
"""