NumPy Input and Output: ndarray.tofile() function
The ndarray.tofile() function is used to write array to a file as text or binary (default).
Data is always written in 'C' order, independent of the order of a.
ndarray.tofile(fid, sep="", format="%s")
|fid||An open file object, or a string containing a filename.
file or str
|sep||Separator between array items for text output. If "" (empty), a binary file is written, equivalent to file.write(a.tobytes()).
|format||Format string for text file output. Each entry in the array is formatted to text by first converting it to the closest Python type, and then using "format" % item.
This is a convenience function for quick storage of array data.
Information on endianness and precision is lost, so this method is not a good choice for files intended to archive data or transport data between machines with different endianness.
Some of these problems can be overcome by outputting the data as text files, at the expense of speed and file size.
When fid is a file object, array contents are directly written to the file, bypassing the file object's write method.
As a result, tofile cannot be used with files objects supporting compression (e.g., GzipFile) or file-like objects that do not support fileno() (e.g., BytesIO).
NumPy.fromstring() method Example:
>>> import numpy as np >>> np.array([1,300],np.int32).tofile('test1') >>> with open('test1','rb') as x: print(x.read())
Python - NumPy Code Editor:
- Exercises: Weekly Top 16 Most Popular Topics
- SQL Exercises, Practice, Solution - JOINS
- SQL Exercises, Practice, Solution - SUBQUERIES
- Java Array: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : Conditional Statement
- HR Database - SORT FILTER: Exercises, Practice, Solution
- C Programming Exercises, Practice, Solution : String
- Python Data Types: Dictionary - Exercises, Practice, Solution
- Python Programming Puzzles - Exercises, Practice, Solution
- C++ Array: Exercises, Practice, Solution
- C# Sharp Basic Algorithm: Exercises, Practice, Solution
- Python Lambda - Exercises, Practice, Solution
- Python Pandas DataFrame: Exercises, Practice, Solution
- Conversion Tools