How To Write A Multidimensional Array To A Text File?
Last Updated :
24 Jan, 2024
In this article, we will explain how to write a multidimensional array to a text file with the help of a few examples. Writing a multidimensional array to a text file is a straightforward process, and it allows you to print the array directly to the text file in a simple manner.
Write A Multidimensional Array To A Text File
To write a multidimensional array to a text file in Python, follow the steps below
Create a Virtual Environment
Create the virtual environment using the below command.
python -m venv env
.\env\Scripts\activate.ps1
Create an Output File
Create a Text file in the same folder for printing the Multidimensional Array.
File Structure
Code Example
Example 1: Write Two Dimensional Array To A Text File
In this example, below code creates a 2D array named two_d_array
, converts it to a formatted string (array_string
) with rows separated by newline and elements within each row separated by a tab, then writes the string to a file named ‘output.txt’. Finally, it prints the original 2D array and a message indicating that the output has been written to ‘output.txt’.
Python3
two_d_array = [
[ 1 , 2 , 3 ],
[ 4 , 5 , 6 ],
[ 7 , 8 , 9 ]
]
array_string = '\n' .join([ '\t' .join( map ( str , row)) for row in two_d_array])
with open ( 'output.txt' , 'w' ) as file :
file .write(array_string)
print ( "2D Array:" )
for row in two_d_array:
print (row)
print ( "\nOutput Written to output.txt" )
|
Output :
1, 2, 3
4, 5, 6
7, 8, 9
Example 2: Write Three Dimensional Array To A Text File
In this example, below Python code defines a three-dimensional array with dimensions 2x3x4, initializes it with sequential values, and then writes the array to an “output.txt” file. The array is created using nested list comprehensions, filled with values incrementally, and each element is written to the file with space separation. Two additional lines of code are suggested to add a newline after each row and a blank line after each second dimension in the file.
Python3
first_dimension = 2
second_dimension = 3
third_dimension = 4
three_dimensional_array = [[[ 0 for _ in range (third_dimension)]
for _ in range (second_dimension)]
for _ in range (first_dimension)]
value = 1
for i in range (first_dimension):
for j in range (second_dimension):
for k in range (third_dimension):
three_dimensional_array[i][j][k] = value
value + = 1
with open ( "output.txt" , "w" ) as file :
for i in range (first_dimension):
for j in range (second_dimension):
for k in range (third_dimension):
file .write(f "{three_dimensional_array[i][j][k]} " )
file .write( "\n" )
file .write( "\n" )
print ( "\nOutput Written to output.txt" )
|
Output :
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
17 18 19 20
21 22 23 24
Conclusion
In Python, the process of writing a multidimensional array to a text file involves initially converting the array into a string representation and subsequently saving it using file handling functions. This tutorial covers fundamental concepts of arrays, file management, and the necessary steps to accomplish this task. We demonstrated this process with a basic 2D list, converting it into a string and writing the result to a file to exemplify handling a 2D array
Share your thoughts in the comments
Please Login to comment...