If it is just a basic .txt file where the values are separated by some delimiter with no headers or anything then all you need to do is
def get_data(filename, columnnumber, delimiter=','):
with open(filename, 'r') as file:
data = file.readlines()
retrieved_data = [x.strip().split(delimiter)[columnnumber] for x in data]
return retrieved_data
Modify the delimiter as needed based on how the data is defined in your .txt file. ex: this code would work for rows defined like 13,34
line 2 and 3: the with open line is used to ensure that your file always closes cleanly. It's good practice to open files this way.
line 4: consider the line '13,34\n' read from the file stored in x. x.strip() removes the new line character \n so we're left with '13,34' and the split function splits as the ',' character so it yields list_temp = ['13', '34']. Then you use list indexing to pull out the value so list_temp[0] or list_temp[1]. the for x in data part just applies the above explanation to every line read into the data list variable
For ease of understanding the below code would yield the same results:
def get_data(filename, columnnumber, delimiter=','):
with open(filename, 'r') as file:
data = file.readlines()
retrieved_data = []
for line in data:
line = line.strip()
row_vals = line.split(delimiter)
col_val = row_vals[columnnumber]
retrieved_data.append(col_val)
return retrieved_data