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

  • Context: Python 
  • Thread starter Thread starter Apashanka
  • Start date Start date
  • Tags Tags
    Arrays File
Click For Summary

Discussion Overview

The discussion revolves around the challenge of printing two equal-sized 2-D arrays into a single .txt file using numpy. Participants explore various approaches and seek clarification on the encountered issues, including error messages and formatting concerns.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Exploratory

Main Points Raised

  • One participant reports attempting to use np.savetxt('data.txt',(a,b)) but encounters a failure, seeking assistance.
  • Another participant requests an example of the input and output to better understand the issue.
  • Clarification is sought regarding the nature of the failure, specifically whether it results in a Python error message or an undesirable format.
  • A suggestion is made to write a program that loops through indices to print the arrays side by side, including a calculation of differences.
  • A participant points out a potential syntax error in the original function call, noting that there is no function named npsavetext and that savetxt is limited to saving a single array at a time.
  • Documentation for numpy's savetxt function is referenced, highlighting its limitations and suggesting that the function should be called twice to save each array separately.

Areas of Agreement / Disagreement

Participants express differing views on the functionality of numpy's savetxt and the correct syntax. There is no consensus on a definitive solution to the original problem, as multiple approaches and interpretations are presented.

Contextual Notes

Limitations include the unclear nature of the error messages encountered and the specific formatting requirements for the output file. The discussion does not resolve these issues.

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   Reactions: Apashanka
Apashanka said:
Okay I will post it...
We're waiting...
 
  • Like
Likes   Reactions: 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   Reactions: jedishrfu, sysprog and Apashanka

Similar threads

  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 2 ·
Replies
2
Views
22K
  • · Replies 6 ·
Replies
6
Views
1K
  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K