value error when using numpy.savetxt

2024/9/19 9:56:42

I want to save each numpy array (A,B, and C) as column in a text file, delimited by space:

import numpy as npA = np.array([5,7,8912,44])B = np.array([5.7,7.45,8912.43,44.99])C = np.array([15.7,17.45,18912.143,144.99])np.savetxt('test.txt', (A, B, C), fmt='%s %s %s')

But I got following error:

ValueError: fmt has wrong number of % formats: %s %s %s

How to solve it?

Answer
np.savetxt('/tmp/test.txt', np.column_stack((A, B, C)), fmt='%s %s %s')

yields

5.0 5.7 15.7
7.0 7.45 17.45
8912.0 8912.43 18912.143
44.0 44.99 144.99

Note that fmt='%s' produces the same result.


If you try

np.savetxt('/tmp/test.txt', (A, B, C))

you'll see that NumPy is writing each 1-D array on a separate line -- i.e. horizontally. Since fmt='%s %s %s' is used as the format for each line, an error was raised since each line had 4 values.

We can get around that problem by passing a 2-D array, np.column_stack((A, B, C)) to np.savetxt.

https://en.xdnf.cn/q/72860.html

Related Q&A

How do I retrieve key from value in django choices field?

The sample code is below:REFUND_STATUS = ((S, SUCCESS),(F, FAIL) ) refund_status = models.CharField(max_length=3, choices=REFUND_STATUS)I know in the model I can retrieve the SUCCESS with method get_re…

Errno13, Permission denied when trying to read file

I have created a small python script. With that I am trying to read a txt file but my access is denied resolving to an no.13 error, here is my code:import time import osdestPath = C:\Users\PC\Desktop\N…

How to write large JSON data?

I have been trying to write large amount (>800mb) of data to JSON file; I did some fair amount of trial and error to get this code:def write_to_cube(data):with open(test.json) as file1:temp_data = j…

Using absolute_import and handling relative module name conflicts in python [duplicate]

This question already has answers here:How can I import from the standard library, when my project has a module with the same name? (How can I control where Python looks for modules?)(7 answers)Close…

Setting results of torch.gather(...) calls

I have a 2D pytorch tensor of shape n by m. I want to index the second dimension using a list of indices (which could be done with torch.gather) then then also set new values to the result of the index…

Why does PyCharm use double backslash to indicate escaping?

For instance, I write a normal string and another "abnormal" string like this:Now I debug it, finding that in the debug tool, the "abnormal" string will be shown like this:Heres the…

Execute Shell Script from Python with multiple pipes

I want to execute the following Shell Command in a python script:dom=myserver cat /etc/xen/$myserver.cfg | grep limited | cut -d= -f2 | tr -d \"I have this:dom = myserverlimit = subprocess.cal…

Python: Is there a way to split a string of numbers into every 3rd number?

For example, if I have a string a=123456789876567543 could i have a list like...123 456 789 876 567 543

How to override the __dir__ method for a class?

I want to change the dir() output for my class. Normally, for all other objects, its done by defining own __dir__ method in their class. But if I do this for my class, its not called.class X(object):de…

Equivalent of wget in Python to download website and resources

Same thing asked 2.5 years ago in Downloading a web page and all of its resource files in Python but doesnt lead to an answer and the please see related topic isnt really asking the same thing.I want t…