In this post, we are going to learn how to save 2D arrays to text files. We will use python standard open() method,savetxt() method that is used to save numpy array to text or CSV file
1. NumPy Savetxt 2D array to text file
This Python program example of how to store a 2D numpy array in a text file. We have used the numpy module savetxt() method. To use this function make sure numpy is installed on our system.
- Import numpy module using code “import numpy as np”
- Convert list of lists to numpy array by using np.array()
- Use the Savetxt() method with parameters filename,array,fmt to write numpy array to text file.
- fmt : %s for string
- %d for integer
- %f for float
- fmt : %s for string
- Once we have saved numpy array to text file we can read it using np.genfromtxt() with specified agruments
import numpy as np
arr = np.array([['Tom', 9, 8, 'WA'],['Trex', 6, 15, 'CA'],
['Kity', 7, 11, 'WA']])
np.savetxt("data.txt", arr,fmt="%s")
content = np.genfromtxt('data.txt', delimiter=',', dtype=None,encoding = 'UTF-8')
print("\ndata.txt conetnt:\n", content)
Output
data.txt conetnt:
['Tom 9 8 WA' 'Trex 6 15 CA' 'Kity 7 11 WA']
2. Numpy savetxt mutiple arrays to text file
In this Python example, we will understand how to store multiple 1D or 2D arrays in a text file. The numpy. savetxt() method is used along with parameters filename, delimiter, type, and fmt. Once the file is saved into a text file we can read it using genfromtxt() method
import numpy as np
arr = np.array([['Tom', 9, 8, 'WA'],['Yack', 6, 15, 'CA'],
['Kity', 7, 11, 'WA']])
arr2 = np.array([['Rack', 9, 8, 'India'],['Trex', 6, 15, 'CA']])
arr3 = np.array(['cat',12.15,'Japan'])
np.savetxt("data.txt", (arr,arr2,arr3),fmt="%s")
content = np.genfromtxt('data.txt', delimiter=',', dtype=None,encoding = 'UTF-8')
print("\ndata.txt conetnt:\n", content)
Output
data.txt conetnt:
["[['Tom' '9' '8' 'WA']" "['Yack' '6' '15' 'CA']"
"['Kity' '7' '11' 'WA']]" "[['Rack' '9' '8' 'India']"
"['Trex' '6' '15' 'CA']]" "['cat' '12.15' 'Japan']"]
3. Python write NumPy 2D array to text file
In this Python program example we are using the open() method with mode ‘w+’ and writing the NumPy 2d array to text file ‘data.txt’.
Once the contents of the numpy array are written. We are reading it using the contents of the file by opening the file in reading mode.”r” and reading the contents of the file. This is how we save the NumPy array to a text file.
#Python program to write Numpy array to text file
import numpy as np
arr = np.array([['Tom', 9, 8, 'WA'],['Yack', 6, 15, 'CA'],
['Kity', 7, 11, 'WA']])
with open("data.txt","w+") as file:
content = str(arr)
file.write(content)
with open("data.txt","r") as file:
content = file.read()
print("\ndata.txt conetnt:\n", content)
Output
data.txt conetnt:
[['Tom' '9' '8' 'WA']
['Yack' '6' '15' 'CA']
['Kity' '7' '11' 'WA']]
4.Python write NumPy 2D multiple arrays to text file
In this Python program example, we are using the python file open() method with statement to write NumPy array to a text file. We are writing multiple numpy arrays to a text file by using the open() method.
- First we have create multiple Numpy arrays from list of list using np.array()
- To joining multiple arrays we are using join() method along with map()
- using the python file open() method with statement to read NumPy array to text file.We are content of text file by using the open() method in read mode.
import numpy as np
arr = np.array([['Tom', 9, 8, 'WA'],['Yack', 6, 15, 'CA'],
['Kity', 7, 11, 'WA']])
arr2 = np.array([['Rack', 9, 8, 'India'],['Trex', 6, 15, 'CA']])
arr3 = np.array(['cat',12.15,'Japan'])
with open("data.txt","w+") as file:
content = str(arr)
file.write("\n".join(" ".join(map(str, x)) for x in (arr,arr2,arr3)))
with open("data.txt","r") as file:
content = file.read()
print("\ndata.txt conetnt:\n", content)
Output
data.txt conetnt:
['Tom' '9' '8' 'WA'] ['Yack' '6' '15' 'CA'] ['Kity' '7' '11' 'WA']
['Rack' '9' '8' 'India'] ['Trex' '6' '15' 'CA']
cat 12.15 Japan
5. NumPy savetxt 2D array row-wise
In this program we are saving the numpy array to text file row-wise using the Python’s open() method
import numpy as np
arr = np.array([['Tom', 9, 8, 'WA'],['Trex', 6, 15, 'CA'],
['Kity', 7, 11, 'WA']])
with open("data1.txt", "w") as file:
for row in arr:
np.savetxt(file, row,fmt="%s")
content = np.genfromtxt('data1.txt', delimiter=',', dtype=None,encoding = 'UTF-8')
print("\ndata.txt conetnt:\n", content)
Output
data.txt conetnt:
['Tom' '9' '8' 'WA' 'Trex' '6' '15' 'CA' 'Kity' '7' '11' 'WA']
Summary
In this post, we have learned how to NumPy Savetxt 2D array to a text file with practical code example using Python NumPy built-in function savetxt() and loadtxt().