. Advertisement .
. Advertisement .
Undoubtedly, reading and writing files are vital knowledge that all programmers must possess. That is why we get various ways to write array to file in Python. However, not many people know everything about them, leading to awkward moments where the code doesn’t work.
To help with this issue, we prepared a clear and concise guide for you.
How To Write Array To File In Python With open()
Many people have the wrong assumption that
open() is only capable of opening files. However, due to its not being restricted by seclusion usage, you can use it in combination with other functions such as
As a result, you can do a bunch of file operations, including modifying, writing, and overwriting. Below is an example of us creating a new NumPy array and then writing its content into a file with the help of the
import numpy as np sample_list = [23, 22, 24, 25] new_array = np.array(sample_list) # Displaying the array file = open("ittutoria.txt", "w+") # Saving the array in a text file content = str(new_array) file.write(content) file.close() # Displaying the contents of the text file file = open("ittutoria.txt", "r") content = file.read() print("Array contents in ittutoria.txt: ", content) file.close()
As you can see, we first created the file and gave it a name utilizing
open(). After this step, we need to move on to converting this array and give it a string format. You need to perform it before attempting to use
write() to record the content, or else you only get an error message.
Once the program finishes running, you can see the text file both on your terminal and in the physical file.
A huge strength of this method is that it is not restricted to single-dimension arrays. Due to
str()’s strength, you can turn even multi-dimensional arrays into strings. From then on, there is nothing stopping
write() from recording the content.
The only thing you must always keep in mind is not to forget to include
close() at the end. Otherwise, the code won’t work. This issue is as problematic as truncating a float.
How To Write Array To File In Python With Content Manager
An alternative approach to this issue is to utilize the content manager to write array to file in Python. It doesn’t have the same weakness as the method above regarding the need of adding
close() at the end.
import numpy as np new_list = [23, 25, 27, 29, 30] new_array = np.array(new_list) print(new_array) with open("ittutoria.txt", "w+") as f: data = f.read() f.write(str(new_array))
In other words, you can decide exactly when you open or close files in accordance with your needs. That is why people believe this solution is generally better, especially if you manage resources.
The only thing you need to implement the content manager is the keyword, as the example below shows clearly.
It starts by opening the file or creating this file if it doesn’t exist yet. Again, we turn the array into a string first before we can write it into the file. Once we finish the job and opt-out, there is an automatic process in which the file closes by itself.
Of course, this approach also works with multi-dimensional arrays.
As we mentioned before, knowing file modification is a core skill, and the knowledge of how to write an array to file in Python is vital. That is why you should read through the guide carefully and master all methods mentioned.
Believe us, the future, you will be grateful for this decision.