Numpy savetxt: change decimal separator

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Numpy savetxt: change decimal separator

Markus1234

Hello everyone,

I save data to a file with the following statement:

np.savetxt(fileName, transpose((average_dist, std_deviation, maximum_dist, sum_of_dist)), delimiter = ';', fmt='%6.10f')

is there a possibility to change the decimal seperator from a point to comma ?
And another question I import this file to excel, is there also a possiblity to create a headline for each column, that the file looks like the following example:

average; standard deviation; maximum distance; sum of distances
0,26565; 0,65565; 2,353535; 25, 5656
...
...
...

Thanks,

Markus
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Numpy savetxt: change decimal separator

Gökhan SEVER-2


On Thu, Sep 24, 2009 at 2:07 AM, <[hidden email]> wrote:

Hello everyone,

I save data to a file with the following statement:

np.savetxt(fileName, transpose((average_dist, std_deviation, maximum_dist, sum_of_dist)), delimiter = ';', fmt='%6.10f')

is there a possibility to change the decimal seperator from a point to comma ?
And another question I import this file to excel, is there also a possiblity to create a headline for each column, that the file looks like the following example:

I don't know how to accomplish the first task, but for the latter the following lines should work:

fid = open(fileName, 'w')
fid.write("average; standard deviation; maximum distance; sum of distances")
np.savetxt(fid, transpose((average_dist, std_deviation, maximum_dist, sum_of_dist)), delimiter = ';', fmt='%6.10f')
fid.close()

 

average; standard deviation; maximum distance; sum of distances
0,26565; 0,65565; 2,353535; 25, 5656
...
...
...

Thanks,

Markus
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion




--
Gökhan

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Numpy savetxt: change decimal separator

Junda Zhu
In reply to this post by Markus1234
On Sep 24, 2009, at 3:07 AM, [hidden email] wrote:

Hello everyone, 

I save data to a file with the following statement: 

np.savetxt(fileName, transpose((average_dist, std_deviation, maximum_dist, sum_of_dist)), delimiter = ';', fmt='%6.10f') 

is there a possibility to change the decimal seperator from a point to comma ? 
And another question I import this file to excel, is there also a possiblity to create a headline for each column, that the file looks like the following example: 

average; standard deviation; maximum distance; sum of distances 
0,26565; 0,65565; 2,353535; 25, 5656 

For the first task, I don't know if there is any direct way in numpy to change the decimal sep, but a little bit awkward trick as follows should work:


mem_file = StringIO.StringIO()
np.savetxt(mem_file, ... )
new_data_str = mem_file.getvalue().replace('.', ',')

output_file = open(fileName, 'w')
output_file.write(new_data_str)
output_file.close()

Or you can use regex to get better match for the decimal seperator.

Thanks,
Junda

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Re: Numpy savetxt: change decimal separator

Timmie
Administrator
>> And another question I import this file to excel, is there also a
>> possiblity to create a headline for each column, that the file looks
>> like the following example:  
>>
>> average; standard deviation; maximum distance; sum of distances  
>> 0,26565; 0,65565; 2,353535; 25, 5656  
I was fiddeling with the same problem here:
http://thread.gmane.org/gmane.comp.python.numeric.general/23418

So far, one can only open the file and prepend the header line.

I just files an enhancement request for this:
proposal: add a header and footer function to numpy.savetxt
http://projects.scipy.org/numpy/ticket/1236

Regards,
Timmie

_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion
Reply | Threaded
Open this post in threaded view
|

Antwort: Re: Numpy savetxt: change decimal separator

Markus1234

>> I was fiddeling with the same problem here:
>> http://thread.gmane.org/gmane.comp.python.numeric.general/23418
>>
>> So far, one can only open the file and prepend the header line.
>>
>> I just files an enhancement request for this:
>> proposal: add a header and footer function to numpy.savetxt
>> http://projects.scipy.org/numpy/ticket/1236
>>
>> Regards,
>> Timmie

Hi Timmie,

thanks for that, this would be a very good first step, still having the problem, that the local representation of the decimal point is not covered...
Of course the porblem can be handeled by a further file parser and a remove('.',',') method, but it looses a bit of the "straight forward" way.



Markus
_______________________________________________
NumPy-Discussion mailing list
[hidden email]
http://mail.scipy.org/mailman/listinfo/numpy-discussion