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

  • Python
  • Thread starter Apashanka
  • Start date
  • #1
429
15

Main Question or Discussion Point

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
 

Answers and Replies

  • #2
1,785
139
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 ?
 
  • #3
jtbell
Mentor
15,584
3,561
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.
 
  • #4
429
15
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...
 
  • #5
11,884
5,533
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
  • #7
429
15
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)
 
  • #8
429
15
We're waiting...
Sorry for my inconvenience I will definitely post it ....(at most Friday)
 
  • #9
33,722
5,418
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

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

  • Last Post
Replies
11
Views
6K
Replies
11
Views
31K
Replies
6
Views
3K
Replies
7
Views
7K
Replies
1
Views
2K
  • Last Post
Replies
4
Views
5K
Replies
11
Views
942
Replies
9
Views
10K
  • Last Post
Replies
11
Views
1K
Replies
12
Views
13K
Top