I have a csv file with 2 columns and 11 rows only (one header row and 10 number rows).
I use custom function to fetch data from the csv file using csv library. The csv file outputs column1 or column2 as a list based on user's choice. The input parameters are:
name_string
: it is the name of the column the user desiresdata_file
: it is the csv file which is opened by using csv library
The function is:
def fetch(name_string, data_file):
x = 0
location = 0
output = []
if name_string == "ONE":
location = 0
elif name_string == "TWO":
location = 1
else:
return print("An unknown error occurred")
for line in data_file:
if x > 0:
output.append(float(line[location]))
x += 1
return output
While using the function inside the code, the function works just fine but only once. On second time it returns an empty list []. I still can't understand why this is happening?
The code is:
import csv
def fetch(name_string, data_file):
x = 0
location = 0
output = []
if name_string == "ONE":
location = 0
elif name_string == "TWO":
location = 1
else:
return print("An unknown error occurred")
for line in data_file:
if x > 0:
output.append(float(line[location]))
x += 1
return output
with open('test_file.csv', 'r') as data1:
rawData = csv.reader(data1)
Col1 = fetch("ONE", rawData)
Col2 = fetch("TWO", rawData)
print(Col1)
print(Col2)
Col1 outputs just fine but Col2 outputs as [].