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:
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook