1

I have some numeric numbers ( about 550,000) and I tried to save them in a CSV file. My values can only be 1 or 2. But it is saved as:

['2.000000000000000000e+00']
['1.000000000000000000e+00']
['2.000000000000000000e+00']
['2.000000000000000000e+00']
...

My code is:

import numpy as np

def save_in_scv_format(My_Labels):
    K = []
    for i in range(len(My_Labels)):
        K.append(My_Labels[i])

    np.savetxt('My_labels.csv', K, delimiter = ',')

My_labels is a vector having integer values of 1 or 2 with length 550,000.

How can I save these values as either a 1 or a 2?

Will
  • 24,082
  • 14
  • 97
  • 108
user6352340
  • 107
  • 2
  • 10
  • What is the format you want? The integer representation (e.g. `1`,`2`,`3`), separated by commas? Like what `print(','.join(K))` shows? – jedwards Jul 20 '16 at 08:16
  • 1
    If you do not say what output format you want, I cannot tell you how to get it :-( – Serge Ballesta Jul 20 '16 at 08:16
  • @jedwards @ Serge : I want to have a vector of [1,2,2,2,1,.....,2,1,1] when I read the file and put the values in a list. – user6352340 Jul 20 '16 at 08:17
  • The default format parameter is `fmt='%.18e`, which is why you are getting 18 trailing zeros. You want to pass `fmt=%d` if you just want `int`s – juanpa.arrivillaga Jul 20 '16 at 08:25
  • Also, as jedwards points out, there is no need to build a list from your numpy array. – juanpa.arrivillaga Jul 20 '16 at 08:28
  • And finally, see [this](http://stackoverflow.com/questions/128478/should-python-import-statements-always-be-at-the-top-of-a-module) thread regarding putting `import` statements inside your functions. What do you mean by vector, anyway? What type is My_Labels? Is it not a numpy array? – juanpa.arrivillaga Jul 20 '16 at 08:31

2 Answers2

1

You can change the formatting of numeric values in the output. From the manual:

fmt : str or sequence of strs, optional

   A single format (%10.5f), a sequence of formats, or a multi-format string,
   e.g. ‘Iteration %d – %10.5f’, in which case delimiter is ignored.

So try:

np.savetxt('My_labels.csv', K, delimiter = ',', fmt='%d')

However, there are other problems with this code.

import numpy as np

def save_in_csv_format(My_Labels):
    np.savetxt('My_labels.csv', My_Labels, delimiter = ',', fmt='%d')

This should do exactly the same thing, and be much more efficient.

Will
  • 24,082
  • 14
  • 97
  • 108
0

First thing to point out, assuming My_Labels is a list as you suggest, the indicated section of your code is superfluous:

def save_in_scv_format(My_Labels):
    import numpy as np
    K  = []                                 # <--
    for i in range(len(My_Labels)):         # <--
            K.append(My_Labels[i])          # <--

    np.savetxt('My_labels.csv', K, delimiter = ',')

You'd be just as well off writing:

def save_in_scv_format(My_Labels):
    import numpy as np

    np.savetxt('My_labels.csv', My_Labels, delimiter = ',')

But I don't think you need numpy to do what it seems you want to do. Something like:

def save_in_scv_format(My_Labels):
    with open('My_labels.csv', 'w') as f:
        f.write(','.join(My_Labels))

would likely work, and better.

Alternatively, you could do something like:

def save_in_scv_format(My_Labels):
    with open('My_labels.csv', 'w') as f:
        f.write(str(My_Labels))

which would preserve the enclosing square brackets, and add spaces between the integers.

There is also the csv and pickle modules, you might look into, for alternative means of outputting your list to a file.

n.b. If I misunderstood, and My_Labels is, for example, a numpy array, then something like:

my_array = My_Labels.tolist()

(docs) is a cleaner way of creating a python list.

jedwards
  • 29,432
  • 3
  • 65
  • 92
  • Do you think saving this in an excel file is better or saving that in a text file? previously I tried to save it into a text file but later I was not able to open the text file. – user6352340 Jul 20 '16 at 08:38
  • A text file is much easier to create, and much easier to read back into Python later. If you create a proper .csv file (e.g. using the csv module), you can import that into excel as well and it will split the integers into columns, removing the commas. – jedwards Jul 20 '16 at 08:45
  • The input is a NumPy `ndarray`; using `.savetxt()` is appropriate here. – Will Jul 20 '16 at 08:53