0

I am a total beginner in Python, so sorry if it is trivial but I can't solve the problem.

I have 3 lists containing floats.

mouse_x = [-0.01, -0.01, -0.01, -0.01, -0.01, -0.01, -0.01, -0.01, -0.01, -0.01]
mouse_y = [-0.14888888888888888, -0.14888888888888888, -0.14888888888888888, -0.14888888888888888, -0.14888888888888888, -0.14888888888888888, -0.14888888888888888, -0.14888888888888888, -0.14888888888888888, -0.14888888888888888]
mouse_time = [1.5307196849607863, 1.581636800954584, 1.6135933389887214, 1.6362467749859206, 1.6675526530016214, 1.6996790579869412, 1.7314749069628306, 1.7635557259782217, 1.7962380870012566, 1.826124977960717]

What I want to do is to combine these three variables so that they are the 3 columns of my csv file. It should look the following way:

mouse_x, mouse_y, mouse_time
-0.01, -0.14888888888888888, 1.5307196849607863
-0.01, -0.14888888888888888, 1.581636800954584
-0.01, -0.14888888888888888, 1.6135933389887214
...
...

This is what I have tried:

with open('my_file.csv', 'w', newline='') as f:
    thewriter = csv.writer(f)
    thewriter.writerow(['mouse_x', 'mouse_y', 'mouse_time'])
    counter = 0
    for x in mouse_x_list:
        thewriter.writerows(mouse_x_list[counter], mouse_y_list[counter], mouse_time_list[counter])
        counter +=1

This produces the following error:

TypeError: writerows() takes exactly one argument (3 given)

I will be very grateful for any tip!

luigigi
  • 3,719
  • 1
  • 9
  • 27
  • CSV wirter only allows one argument. Throw it into a loop and write the item from each list in that index. – Jason V Feb 06 '20 at 13:33

6 Answers6

2

You need to change writerows to writerow and wrap the three arguments into a list [...]:

thewriter.writerow ( [mouse_x_list[counter], mouse_y_list[counter], mouse_time_list[counter]])

csv.writerows expects a row object, but writerow also accepts simple lists -- as you used earlier to write out the header.

Unrelated, but you can omit the counter by using zip to weave those three lists into one:

for a,b,c in zip(mouse_x_list,mouse_y_list,mouse_time_list):
    thewriter.writerow ( [a,b,c])
Jongware
  • 21,058
  • 8
  • 43
  • 86
  • 2
    Even simpler would be to omit the loop and just do `thewriter.writerows(zip(mouse_x, mouse_y, mouse_time))` – tfw Feb 06 '20 at 13:48
  • For what it's worth, I've got an example of how to implement tfw's suggestion to use `zip` in my own answer. Looks like tfw and I thought pretty much alike here. – jjramsey Feb 06 '20 at 14:10
1

Would you accept to use numpy? I find it to be very straightforward for this kind of applications:

You generate a multidimensional array for which each column is one of your lists:

import numpy as np

arr = np.array([mouse_x, mouse_y, mouse_time])

In this way you can access mouse_x with arr[:, 0], mouse_y with arr[:, 1] and mouse_time with arr[:, 2].

Once you have this data structure, numpy ships a builtin function to save arrays:

np.savetxt('mouse.csv', arr.T, delimiter=',')
rdbisme
  • 761
  • 1
  • 9
  • 34
1

This is one way you can write your CSV file without using NumPy or Pandas:

with open('my_file.csv', 'w') as f:
    thewriter = csv.writer(f)
    thewriter.writerow(['mouse_x', 'mouse_y', 'mouse_time'])
    len_rows = len(mouse_x)
    for counter in range(len_rows):
        thewriter.writerow([mouse_x[counter], mouse_y[counter], mouse_time[counter]])

Notice that you don't need to increment a counter variable yourself here; the for statement in conjunction with range() already sets counter to 0, 1, and so on. Notice that in the for loop, I'm using writerow not writerows.

An arguably more "Pythonic" way to write the CSV file with a for loop would be to do this:

with open('my_file.csv', 'w') as f:
    thewriter = csv.writer(f)
    thewriter.writerow(['mouse_x', 'mouse_y', 'mouse_time'])
    for mx, my, mtime in zip(mouse_x, mouse_y, mouse_time):
        thewriter.writerow([mx, my, mtime])

Now, writerows() takes an iterable of rows, for example, a list or tuple of rows. An iterable, loosely speaking, is what you'd put after the "in" in a for statement. If you had, say, a list of tuples, where tuple i in the list was (mouse_x[i], mouse_y[i], mouse_time[i]), then you could pass that list of tuples to writerows() and be done with it. Alternatively, one could use zip(mouse_x, mouse_y, mouse_time) from the above for loop itself as an argument of writerows():

with open('my_file.csv', 'w') as f:
    thewriter = csv.writer(f)
    thewriter.writerow(['mouse_x', 'mouse_y', 'mouse_time'])
    thewriter.writerows(zip(mouse_x, mouse_y, mouse_time))

Arguably, that's even more Pythonic, for whatever that's worth.

jjramsey
  • 858
  • 4
  • 13
0

If you want to use pandas:

import pandas as pd
df=pd.DataFrame([mouse_x,mouse_y,mouse_time])
df.T.to_csv('my_file.csv', index=False, header=['mouse_x','mouse_y','mouse_time'])
luigigi
  • 3,719
  • 1
  • 9
  • 27
0

You can also use pandas to make a dataframe first and than save it as an csv-file:

     import pandas as pd

     df = pd.DataFrame()
     df.insert(0, 'mouse_x', mouse_x)
     df.insert(1, 'mouse_y', mouse_y)
     df.insert(2, 'mouse_time', mouse_time)
     df

output:

        mouse_x mouse_y mouse_time
      0 -0.01   -0.148889   1.530720
      1 -0.01   -0.148889   1.581637
      2 -0.01   -0.148889   1.613593
      3 -0.01   -0.148889   1.636247
      4 -0.01   -0.148889   1.667553
      5 -0.01   -0.148889   1.699679
      6 -0.01   -0.148889   1.731475
      7 -0.01   -0.148889   1.763556
      8 -0.01   -0.148889   1.796238
      9 -0.01   -0.148889   1.826125

save to csv:

     df.to_csv('name_file.csv')
Renate van Kempen
  • 92
  • 1
  • 1
  • 10
0

Numpy is one of the best libraries regarding data stored in arrays. It also includes implementations to save arrays to file directly. Check out how to use it here .