Python To print two equal sized 2-D arrays adjacently in a single .txt file

  • Thread starter Thread starter Apashanka
  • Start date Start date
  • Tags Tags
    Arrays File
AI Thread Summary
The discussion centers on the issue of saving two equal-sized 2-D arrays, referred to as 'a' and 'b', to a single text file using NumPy. The user initially attempts to use the function np.savetxt('data.txt', (a, b)), but encounters a syntax error. Participants in the discussion request clarification on the error, suggesting that the user provide an explicit example of the code and the error message received. It is noted that the correct function is np.savetxt, not npsavetext, and that this function can only save one array at a time. To save both arrays in the same file, the user is advised to call np.savetxt twice, once for each array. Additionally, the importance of consulting the NumPy documentation for proper usage and syntax is emphasized.
Apashanka
Messages
427
Reaction score
15
I have two 2-D equal sized arrays (m×n) say a,b and want to print them in a single .txt file and have tried this using numpy
np.savetxt('data.txt',(a,b)) but it fails..,
Can anyone please help in sort out this
Thanks
 
Technology news on Phys.org
Apashanka said:
I have two 2-D equal sized arrays (m×n) say a,b and want to print them in a single .txt file and have tried this using numpy
np.savetxt('data.txt',(a,b)) but it fails..,
Can anyone please help in sort out this
Thanks
Can you show us an example for an input and output ?
 
Apashanka said:
have tried this using numpy
np.savetxt('data.txt',(a,b)) but it fails..,
How exactly does it fail? Does it give you a Python error message, or does it display the arrays in a format which isn't what you want? An explicit example, and your code, would heip us.
 
jtbell said:
How exactly does it fail? Does it give you a Python error message, or does it display the arrays in a format which isn't what you want? An explicit example, and your code, would heip us.
Okay I will post it...
 
I would simply write a program to loop through the indices and print them side by side into these column breakdowns:

(i) :: (j) :: A(i,j) :: B(i,j) :: Diff B - A

You could even count the number of nonzero differences if that is what you’re after.
 
  • Like
Likes Apashanka
Apashanka said:
Okay I will post it...
We're waiting...
 
  • Like
Likes jedishrfu
jtbell said:
How exactly does it fail? Does it give you a Python error message, or does it display the arrays in a format which isn't what you want? An explicit example, and your code, would heip us.
It shows a python error message showing that there is a problem in the syntax .npsavetext('filename.txt',(a,b)) where a and b are two different 2-D arrays...
Sorry for my inconvenience I will post it in the next few days (atmost Friday)
 
Mark44 said:
We're waiting...
Sorry for my inconvenience I will definitely post it ...(at most Friday)
 
Apashanka said:
It shows a python error message showing that there is a problem in the syntax .npsavetext('filename.txt',(a,b)) where a and b are two different 2-D arrays...
I don't believe there is a numpy (or scipy) function named either npsavetext or .npsavetext. There is a function named savetext(), but it is limited to saving a single one- or two-dimension array to a text file.

Based on the documentation I found (https://docs.scipy.org/doc/numpy/reference/generated/numpy.savetxt.html), here is an example of how to use savetext().
Python:
import numpy as np
a = [1, 2, 3]
np.savetext('filename.txt', a)  # Last 7 parameters are omitted

The savetext() function takes 9 arguments, of which only the first two are required -- the text file that will receive the elements of the array, and the array itself. There is no way to use this function to save the contents to two arrays in one call to the function, but you should be able to call the function twice to save each array in the same file.

When you are using a library like numpy or scipy, and you're getting syntax errors when you call the library function, the very first place you should look for guidance is in the documentation for the library function.
 
Last edited:
  • Like
Likes jedishrfu, sysprog and Apashanka

Similar threads

Replies
13
Views
2K
Replies
2
Views
22K
Replies
6
Views
1K
Replies
9
Views
1K
Replies
8
Views
2K
Replies
8
Views
1K
Replies
2
Views
1K
Replies
2
Views
2K
Back
Top