Skip to content

Commit f715f27

Browse files
Update saving_numpy_arrays_to_files.md
Added examples Added savetxt method
1 parent e734ba6 commit f715f27

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

contrib/numpy/saving_numpy_arrays_to_files.md

+34-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,40 @@ The `np.savetxt` function saves a NumPy array to a text file, such as `.txt` or
8787
#### Syntax :
8888

8989
```python
90-
numpy.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# ', encoding=None)
90+
numpy.savetxt(fname, X, delimiter=' ', newline='\n', header='', footer='', encoding=None)
9191
```
9292
- **fname** : Name of the file.
9393
- **X** : Array to be saved.
94-
- **kwds** : Named arrays to be saved.
94+
- **delimiter** : It is a Optional parameter,This is a character or string that is used to separate columns.(By Default it is " ")
95+
- **newline** : It is a Optional parameter, Character for seperating lines.(By Default it is "\n")
96+
- **header** : It is a Optional parameter, String that is written at beginning of the file.
97+
- **footer** : It is a Optional parameter, String that is written at ending of the file.
98+
- **encoding** : It is a Optional parameter, Encoding of the output file. (By Default it is None)
99+
100+
#### Example :
101+
102+
```python
103+
import numpy as np
104+
105+
arr = np.array([1.1,2.2,3,4.4,5])
106+
np.savetxt("example.txt",arr) #saves the array in example.txt
107+
108+
```
109+
110+
Inorder to load the array from example.txt
111+
112+
```python
113+
114+
arr1 = np.loadtxt("example.txt")
115+
print(arr1)
116+
117+
```
118+
**Output** :
119+
```python
120+
[1.1 2.2 3. 4.4 5. ]
121+
```
122+
123+
124+
By using these methods, you can efficiently save and load NumPy arrays in various formats suitable for your needs.
125+
126+

0 commit comments

Comments
 (0)