0

I have three lists which I would like to save in a csv file using the savetxt function from numpy.

list1, list2 and list3 all have the same length with a single value.

list1,list2 are integer values, list3 has string values

So in the end it should look like this column1=list1 column2=list2 column3=list3

This works for me if list3 is integer,too. It doesn´t work now, because list3 is a string.

How can I realize this with list3 as a string and find a workaround for c_?

savetxt('data/result.csv', c_(list1,list2,list3), delimiter=',', fmt='%d, %d, %d', 
            header='Name,Value,Value2', comments = '')

Thank you very much for help!

4
  • Your fmt string tries to format them all as integers. Commented Jun 8, 2014 at 13:25
  • I know about this, but it doesnt work. TypeError: 'CClass' object is not callable Commented Jun 8, 2014 at 13:28
  • c_ does something like this: stackoverflow.com/questions/18601001/… docs.scipy.org/doc/numpy/reference/generated/numpy.c_.html Commented Jun 8, 2014 at 13:50
  • If the data is in python lists, why not use the csv module? E.g. f = open('result.csv', 'w'); wrtr = csv.writer(f); wrtr.writerows(zip(list1, list2, list3)); f.close() Commented Jun 8, 2014 at 13:52

1 Answer 1

2

One problem is this:

c_(list1, list2, list3)

c_ is a quirky object that is used by indexing it instead of calling it. When you call it, you get the error mentioned in the comments:

In [42]: c_(list1, list2, list3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-42-8fcbef857684> in <module>()
----> 1 c_(list1, list2, list3)

TypeError: 'CClass' object is not callable

Instead, use square brackets, so you are indexing:

In [44]: c_[list1, list2, list3]
Out[44]: 
array([['10', '15', 'foo'],
       ['20', '25', 'bar'],
       ['30', '35', 'baz']], 
      dtype='|S3')

Note that it creates an array with dtype '|S3'. That is, all the elements in the array have been converted to strings. To save this with savetxt, use fmt='%s, %s, %s':

In [45]: savetxt('result.csv', c_[list1, list2, list3], fmt='%s, %s, %s')

In [46]: !cat result.csv
10, 15, foo
20, 25, bar
30, 35, baz

Also, instead of c_[list1, list2, list3], you could use zip(list1, list2, list3). Then the savetxt function will handle converting that argument to an array.

In [57]: list1 = [100000, 200000, 300000]

In [58]: savetxt('result.csv', zip(list1, list2, list3), fmt='%s, %s, %s')

In [59]: !cat result.csv
100000, 15, foo
200000, 25, bar
300000, 35, baz

Apparently c_ doesn't do a good job figuring out the appropriate lengths of the strings:

In [60]: c_[list1, list2, list3]
Out[60]: 
array([['100', '15', 'foo'],
       ['200', '25', 'bar'],
       ['300', '35', 'baz']], 
      dtype='|S3')

By the way, your data is not already in a numpy array, so I don't think you gain much by using savetxt instead of the standard library csv. For example,

In [61]: import csv

In [62]: with open('result.csv', 'w') as f:
   ....:     wrtr = csv.writer(f)
   ....:     wrtr.writerows(zip(list1, list2, list3))
   ....:     

In [63]: !cat result.csv
100000,15,foo
200000,25,bar
300000,35,baz
Sign up to request clarification or add additional context in comments.

7 Comments

We are getting closer, I have tried this, thanks. But all my integer numbers 10000 are converted to 1.
all my integers are now 1-9. This starts when using c_[] transformed_matrix =c_[list1,list2,list3] print(transformed_matrix[1] => ['1' '2' 'b']
I updated my answer to suggest using zip(list1, list2, list3) instead of c_[list1, list2, list3].
Just one question, I have done one csv file manually with integers and replacing the last column with string manually. And now compared it to %s %s %s. How come this file is larger?
Use the fmt argument to control the spacing, e.g. %s,%s,%s vs. %s, %s, %s
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.