Updated April 19, 2023
Introduction to NumPy savetxt
Python provides different functions to the users. To work with arrays, the python library provides a NumPy function. Mainly NumPy savetxt function is used to save arrays in txt format with different delimiters. NumPy savetxt function works with 1D and 2D arrays, the numpy savetxt also saves array elements in csv file format. Arrays play a major role in data science where speed matters. NumPy is an acronym for numerical python. Basically, NumPy is an open source project. NumPy performs logical and mathematical operations of arrays. Therefore, processing and manipulating can be done efficiently.
Syntax of NumPy savetxt
Given below is the syntax:
numpy.savetxt(file_name, Array, format='%.4e', delimiter=' ', newline='n', header=' ', footer=' ', comments='# ')
Explanation:
- file_name: file_name is used for actual file name. It means that if a file ends with .txt then the file is automatically saved in text format. In the same manner, it works for different file extensions.
- Arr: Arr means 1D or 2D array and it is used to store array data in a text file.
- format: format parameter is used to define a sequence of patterns. It is used when we want to save data into the text file. If format is specified as single like “%d” that means it is applicable to all the elements. The 2D array specified is different for each column and it is an optional part.
- delimiter: It is used either as a string or character to separate columns and it is an optional parameter of savetxt function.
- newline: The newline parameter is used to separate string or character and it is an optional parameter of savetxt function.
- header: String or character will be written at the beginning of the file with the help of header parameter and it is an optional parameter.
- footer: String or character will be written at the end of file with the help of header parameter and it is an optional parameter.
- comments: comments parameter uses symbol ‘#’ to comment on a particular section. In some versions header and footer are by default assigned as comments.
How savetxt Function work in NumPy?
- We must install Python on our system.
- We must install numpy using the pip command.
- We require basic knowledge about Python.
- We require basic knowledge about the savetxt function with different parameters.
- We require basic knowledge about arrays.
- We can perform different operations using a numpy savetxt function.
Examples of NumPy savetxt
Given below are the examples of NumPy savetxt:
Example #1
For 1D Array.
Code:
import numpy as np
A = np.arange(0, 6, 1)
print("The array of A:")
print(A)
c = np.savetxt('1D.txt', A, delimiter=', ') # save array into txt file
X = open("1D.txt", 'r') # open file in read mode
print("the file contains is:")
print(X.read()) # open file
Explanation:
- We import numpy functions and use them as np.
- We declared variable A for array and assigned values.
- We try to print the value of the variable.
- Then we use the savetxt function to store array values into the txt file.
- After that we open that file in read mode.
- Finally we try to print txt file.
- In the above example we implemented a 1D array using numpy savetxt function.
- Illustrate the end result of the above declaration by using the use of the following snapshot.
Output:
Example #2
For 2D Array.
Code:
import numpy as np
a = np.arange(0, 5, 1)
b = np.arange(5, 10, 1)
c = np.arange(10, 15, 1)
print("Array a is:")
print(a)
print("Array b:is:")
print(b)
print("Array c: is:")
print(c)
x = np.savetxt('demoa.txt', (a, b, c)) # Array with same dimension
y = open("demoa.txt", 'r') # open file in read mode
print("the file contains:")
print(y.read())
Explanation:
- We import numpy functions and use it as np.
- We declared variable a, b and c for array and assigned values.
- We try to print the value of the variable a, b and c.
- Then we use the savetxt function to store array values into the 2d.txt file with same array dimension.
- After that we open that file in read mode.
- Finally we try to print 2d.txt files.
- In the above example we implement a 2D array using numpy savetxt function.
- Illustrate the end result of the above declaration by using the use of the following snapshot.
Output:
Example #3
1D array store into csv file with header and footer.
Code:
import numpy as np
a = np.arange(0, 5, 1)
print("Array a is:")
print(a)
x = np.savetxt('hf.csv', a, delimiter=',' , header='A sample 1D array :: Header', footer='This is footer of csv file') # savetxt function with header and footer.
y = open("hf.csv", 'r') # open file in read mode
print("the file contains following array values:")
print(y.read())
Explanation:
- We import numpy functions and use them as np.
- We declared variable a for array and assigned values.
- We try to print the value of the variable a.
- Then we use the savetxt function to store array values into the hf.csv file with header and footer.
- After that we open that file in read mode.
- Finally we try to print the hf.csv file.
- In the above example we implemented a 1D array using numpy savetxt function with header and footer parameters.
- Illustrate the end result of the above declaration by using the use of the following snapshot.
Output:
Example #4
1D Array with exponential notation.
Code:
import numpy as np
a = b = c = np.arange(0.0,4.0,1.0)
print(a)
np.savetxt('demo.txt', a, delimiter=',')
np.savetxt('demo.txt', (a,b,c))
np.savetxt('demo.txt', a, fmt='%1.4e') # exponential notation
x = open("demo.out", 'r')
print("the file contains following array values:")
print(x.read())
Explanation:
- In the above example we have followed the same process but the only difference is that in this program we have added exponential notation.
- Illustrate the end result of the above declaration by using the use of the following snapshot.
Output:
Conclusion
From the above article we saw basic syntax of numpy savetxt functions. We also saw how we can implement them in python with different examples. From this article we have seen how we can handle numpy savetxt functions in python.
Recommended Articles
This is a guide to NumPy savetxt. Here we discuss the introduction, how savetxt function work in NumPy? along with examples respectively. You may also have a look at the following articles to learn more-