Jakeb Jakeb - 5 months ago 17
Python Question

Extracting data from a list using a for loop

I am quite new to python. For an exercise i am reading in data from a csv file and storing it in a list. Having trouble then accessing elements in the list and storing them as variables thereafter.

The data contained in the file will be an ID, followed by two sets of coordinates

ID, x1, y1, x2, y2


I need to be able to extract the data from my list to be used in calculations later on. hoping to loop over the list for multiple lines to be calculated.

Any help is appreciated!

Cheers

Answer

You should use the csv library

with open('/path/to/file.csv', 'r') as f:
    reader = csv.reader(f)
    for row in reader:
        ID, x1, y1, x2, y2 = row
Comments