NumPy Input and Output: fromfile() function
numpy.fromfile() function
The fromfile() function is used to construct an array from data in a text or binary file.
Syntax:
numpy.fromfile(file, dtype=float, count=-1, sep='')
Version: 1.15.0
Parameter:
Name | Description | Required / Optional |
---|---|---|
file | Open file object or filename. file or str |
Required |
dtype | Data type of the returned array. For binary files, it is used to determine the size and byte-order of the items in the file. data-type |
Required |
count | Number of items to read. -1 means all items (i.e., the complete file). int |
Required |
sep | Separator between items if file is a text file. Empty ("") separator means the file should be treated as binary. Spaces (" ") in the separator match zero or more whitespace characters. A separator consisting only of spaces must match at least one whitespace. str |
Required |
Notes:
Do not rely on the combination of tofile and fromfile for data storage, as the binary files generated are are not platform independent. In particular, no byte-order or data-type information is saved. Data can be stored in the platform independent .npy format using save and load instead.
NumPy.fromfile() method Example:
Construct an ndarray:
>>> import numpy as np
>>> dt = np.dtype([('time', [('min', int), ('sec', int)]),('temp', float)])
>>> x = np.zeros((1,), dtype=dt)
>>> x['time']['min'] = 15; x['temp'] = 95.25
>>> x
Output:
array([((15, 0), 95.25)], dtype=[('time', [('min', '<i4'), ('sec', '<i4')]), ('temp', '<f8')])
Python - NumPy Code Editor:
Previous: ndarray.tolist() function
Next: array2string() function
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/numpy/input-and-output/fromfile.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics