Home > Software engineering >  how can I iterate over rows and update different column indexed variables using python?
how can I iterate over rows and update different column indexed variables using python?

Time:07-21

   content = file.read().strip().split('\n')
   for x in content:
   c_x= content[0]
   print(c_x) #should print new values from the first column but new row until the end of the row
   c_y=content[1]
   print(c_y) #should print new values from the second column but new row until the end of the row
   c_z=content[2]
   print(c_z) # should print new values from the third column but new row until the end of the row

also if I write print(x) it prints all the contents line by line but I am trying to store those values in each variable and update variable when line changes using python the contents are as follows. columns are c_x, c_y, c_z.

    c_x , c_y  , c_z

row1 '1' , '2'  ,  '3'    \n

row2 '4' , '5'  ,  '6'    \n

row3 '7' , '8'  ,  '9'    \n

row4 '10', '11' ,  '12'   \n

I hope this helps better now I have a content from csv in a variable named content.

CodePudding user response:

a = [1, 2, 3, 4, 5, 8, 3, 3, 1]
for i in range(0, len(a), 3):
    x = a[i]
    y = a[i   1]
    z = a[i   2]
    print(x, y, z)
    print([x, y, z])

I hope it will help you. Correct me if wrong.

CodePudding user response:

You can try This:

content= [[1,2,3], [4,5,8],[3,3,1]]
for x in content:
    a, b, c = [x[i] for i in range(len(x))]
    
print(a)
print(b)
print(c)

output:

3
3
1
  • Related