I am quite new to python and currently trying to work on a project that asks the user to read data from a csv file, and to store that data so that other functions can be performed on it. The only issue I seem to be having at the moment is that I can't use global variables.
Just now I have the following code structure:
import csv
import sys
data_set = []
def loadFile(x):
with open(x, "r") as readfile:
csv_reader = csv.reader(readfile, delimiter= ';')
for row in csv_reader:
data_set.append(row)
print("Loaded weather data from", (x[0:-4]).capitalize())
print()
def avgDay(x):
for line in data_set:
if(len(x) == 5 and (x[3:5] + "-" + x[0:2]) in line[0]):
print("The weather on", x, "was on average", line[2], "centigrade")
Is there some way I can call data_set to other functions? (I have a few more functions that need to work with the data).
Yes, simply pass it in as a parameter, and return it when it is originally generated.
import csv
import sys
def loadFile(x):
date_set = []
with open(x, "r") as readfile:
csv_reader = csv.reader(readfile, delimiter= ';')
for row in csv_reader:
data_set.append(row)
print("Loaded weather data from", (x[0:-4]).capitalize())
print()
return data_set
def avgDay(x, data_set):
for line in data_set:
if(len(x) == 5 and (x[3:5] + "-" + x[0:2]) in line[0]):
print("The weather on", x, "was on average", line[2], "centigrade")
def main():
data_set = loadFile(...)
avgDay(..., data_set)
if __name__ == 'main':
main()