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

  • Thread starter Apashanka
  • Start date
392
13
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
 
1,553
122
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 ?
 

jtbell

Mentor
15,354
3,082
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.
 
392
13
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...
 
10,830
4,359
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.
 
392
13
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)
 
32,635
4,380
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:

Want to reply to this thread?

"To print two equal sized 2-D arrays adjacently in a single .txt file" You must log in or register to reply here.

Related Threads for: To print two equal sized 2-D arrays adjacently in a single .txt file

  • Posted
Replies
11
Views
6K
Replies
11
Views
29K
Replies
6
Views
3K
Replies
7
Views
7K
Replies
1
Views
2K
Replies
11
Views
807
  • Posted
Replies
4
Views
5K
Replies
9
Views
10K

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top