Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Reading and appending list
#1
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,,,,,,
Reply
#2
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  trouble reading string/module from excel as a list popular_dog 0 386 Oct-04-2023, 01:07 PM
Last Post: popular_dog
  Reading list items without brackets and quotes jesse68 6 4,523 Jan-14-2022, 07:07 PM
Last Post: jesse68
  Reading data to python: turn into list or dataframe hhchenfx 2 5,280 Jun-01-2021, 10:28 AM
Last Post: Larz60+
  Time.sleep: stop appending item to the list if time is early quest 0 1,847 Apr-13-2021, 11:44 AM
Last Post: quest
  Appending list Trouble Big Time Milfredo 7 3,025 Oct-01-2020, 02:59 AM
Last Post: Milfredo
  Appending to list of list in For loop nico_mnbl 2 2,320 Sep-25-2020, 04:09 PM
Last Post: nico_mnbl
  while loop reading list in reverse Jerry51 1 1,542 Apr-24-2020, 12:44 PM
Last Post: deanhystad
  appending list of list glennford49 2 2,108 Mar-29-2020, 09:33 AM
Last Post: ibreeden
  Writing list as a file, then reading that file as a list Zoastria_Balnala 3 2,552 Oct-17-2019, 07:54 PM
Last Post: Zoastria_Balnala
  Appending to list not working and causing a infinite loop eiger23 8 3,942 Oct-10-2019, 03:41 PM
Last Post: eiger23

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020