Skip to content

Commit cfc731c

Browse files
Update loading_arrays_from_files.md
Changed introduction content formatted code formatted content
1 parent 53269a5 commit cfc731c

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

contrib/numpy/loading_arrays_from_files.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ and more. It reads the file line by line, splits it at the specified delimiter,
1414

1515
**fname** : Name of the file <br>
1616
**dtype** : Data type of the resulting array. (By default is float) <br>
17-
**delimiter**: String or character separating columns; default is any whitespace. <br>
17+
**delimiter**: String or character separating columns. (By default is whitespace) <br>
1818
**converters**: Dictionary mapping column number to a function to convert that column's string to a float. <br>
1919
**skiprows**: Number of lines to skip at the beginning of the file. <br>
2020
**usecols**: Which columns to read starting from 0.
@@ -27,8 +27,9 @@ and more. It reads the file line by line, splits it at the specified delimiter,
2727

2828
**Code** <br>
2929
```python
30-
import numpy as np
31-
arr = np.loadtxt("loadtxt.txt", dtype=int)
30+
import numpy as np
31+
32+
arr = np.loadtxt("example.txt", dtype=int)
3233
print(arr)
3334
```
3435

@@ -39,20 +40,18 @@ and more. It reads the file line by line, splits it at the specified delimiter,
3940

4041
<br>
4142

42-
### 2. numpy.genfromtxt:
43+
### 2. numpy.genfromtxt():
4344
The `genfromtxt` function is similar to loadtxt but provides more flexibility. It handles missing values (such as NaNs), allows custom converters
4445
for data parsing, and can handle different data types within the same file. It’s particularly useful for handling complex data formats.
4546

4647
- #### Syntax:
4748
```python
48-
numpy.genfromtxt(fname, dtype=float, delimiter=None, skip_header=0, skip_footer=0, converters=None, missing_values=None, filling_values=None, usecols=None)
49+
numpy.genfromtxt(fname, dtype=float, delimiter=None, converters=None, missing_values=None, filling_values=None, usecols=None)
4950
```
5051

5152
**fname** : Name of the file <br>
5253
**dtype** : Data type of the resulting array. (By default is float) <br>
5354
**delimiter**: String or character separating columns; default is any whitespace. <br>
54-
**skip_header**: Number of lines to skip at the beginning of the file.<br>
55-
**skip_footer**: Number of lines to skip at the end of the file.<br>
5655
**converters**: Dictionary mapping column number to a function to convert that column's string to a float. <br>
5756
**missing_values**: Set of strings corresponding to missing data.<br>
5857
**filling_values**: Value used to fill in missing data. Default is NaN.<br>
@@ -67,7 +66,8 @@ for data parsing, and can handle different data types within the same file. It
6766

6867
**Code** <br>
6968
```python
70-
import numpy as np
69+
import numpy as np
70+
7171
arr = np.genfromtxt("example.txt", dtype='str', usecols=1)
7272
print(arr)
7373
```
@@ -80,7 +80,7 @@ for data parsing, and can handle different data types within the same file. It
8080
<br>
8181

8282

83-
### 3. numpy.load
83+
### 3. numpy.load():
8484
`load` method is used to load arrays saved in NumPy’s native binary format (.npy or .npz). These files preserve the array structure, data types, and metadata.
8585
It’s an efficient way to store and load large arrays.
8686

@@ -90,18 +90,19 @@ It’s an efficient way to store and load large arrays.
9090
```
9191

9292
**fname** : Name of the file <br>
93-
**mmap_mode** : Memory-map the file using the given mode (r, r+, w+, c).(By Default None) <br>
93+
**mmap_mode** : Memory-map the file using the given mode (r, r+, w+, c)(By Default None).Memory-mapping only works with arrays stored in a binary file on disk, not with compressed archives like .npz.<br>
9494
**encoding**:Encoding is used when reading Python2 strings only. (By Default ASCII) <br>
9595

9696
- #### Example for `load`:
9797

9898
**Code** <br>
9999
```python
100100
import numpy as np
101+
101102
arr = np.array(['a','b','c'])
102-
np.savez('data.npz', array=arr)
103-
# stores arr in data.npz in NumPy's native binary format
104-
data = np.load('data.npz')
103+
np.savez('example.npz', array=arr) # stores arr in data.npz in NumPy's native binary format
104+
105+
data = np.load('example.npz')
105106
print(data['array'])
106107
```
107108

0 commit comments

Comments
 (0)