Python Forum
Reading and appending list - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Reading and appending list (/thread-32725.html)



Reading and appending list - MrSwiss - Mar-01-2021

I have to read code from a .csv file and put specific columns of data into separate lists. However when I get to column 21 and 22 it says there is not a number there when there is when looking at the spreadsheet. Not sure what my error is since columns 0-4 work fine. The pink x and y list work fine but the yellow ones are what Im having trouble with.

pink_x = []
pink_y = []
yellow_x = []
yellow_y = []
#Calibrating Camera

file = open("meter_sticks.csv", 'r')
firstline = True
secondline = True
thirdline = True
fourthline = True
fifthline = True

line = file.readline()
i = 0
while line != '' and i <=1229:
    if firstline:
        firstline = False
        line = file.readline()
        continue
    if secondline:
        secondline = False
        line = file.readline()
        continue
    if thirdline:
        thirdline = False
        line = file.readline()
        continue
    if fourthline:
        fourthline = False
        line = file.readline()
        continue
    if fifthline:
        fifthline = False
        line = file.readline()
        continue
    if line[2] == '':
        line = file.readline()

        continue

    i+=1
    line = line.split(',')


    pink_x.append(int(line[3]))
    pink_y.append(int(line[4]))
    yellow_x.append(int(line[21]))
    yellow_y.append(int(line[22]))
    line = file.readline()
file.close()

print(pink_x)
print(yellow_y)
Here is a portion of the .csv file i'm working with.

Output:
frame_no,timestamp,size_px-hotpink,position_px_x-hotpink,position_px_y-hotpink,rx-hotpink,ry-hotpink,vx-hotpink,vy-hotpink,ax-hotpink,ay-hotpink,size_px-lightorange,position_px_x-lightorange,position_px_y-lightorange,rx-lightorange,ry-lightorange,vx-lightorange,vy-lightorange,ax-lightorange,ay-lightorange,size_px-yellowneon,position_px_x-yellowneon,position_px_y-yellowneon,rx-yellowneon,ry-yellowneon,vx-yellowneon,vy-yellowneon,ax-yellowneon,ay-yellowneon 0,128.6,,,,,,,,,,30,34,133,,,,,,,,,,,,,,, 1,128.6,,,,,,,,,,32,34,135,,,,,,,,,,,,,,, 2,236.1,,,,,,,,,,30,34,134,,,,,,,,,,,,,,, 3,260.1,,,,,,,,,,30,34,133,,,,,,,,,,,,,,, 4,277.0,6,655,627,,,,,,,,,,,,,,,,7,426,431,,,,,, 5,285.7,6,655,627,,,,,,,,,,,,,,,,8,426,431,,,,,, 6,304.6,6,655,627,,,,,,,,,,,,,,,,8,426,431,,,,,, 7,304.6,6,655,627,,,,,,,,,,,,,,,,8,426,431,,,,,, 8,324.9,6,655,627,,,,,,,,,,,,,,,,8,427,431,,,,,, 9,324.9,6,655,627,,,,,,,,,,,,,,,,8,426,431,,,,,,



RE: Reading and appending list - Serafim - Mar-01-2021

It works when I copy your code and the portion of your csv file. There must be some error in another portion of the csv file, maybe a hidden character, such as a control character. It would be helpful to see the full error traceback.