w3resource

NumPy I/O Operations: Exercises, Practice, and Solutions


This resource offers a total of 20 NumPy I/O Operations problems for practice.

[An Editor is available at the bottom of the page to write and execute the scripts.]


1. CSV Numeric Read

Write a NumPy program that reads data from a CSV file into a NumPy array. The CSV file contains numeric data.

Click me to see the sample solution


2. CSV with Mixed Data Write

Write a NumPy array with both numeric and string data to a CSV file.

Click me to see the sample solution


3. Structured Array CSV Read

Write a NumPy program that reads data from a CSV file into a structured NumPy array with specified data types for each column.

Click me to see the sample solution


4. Structured Array CSV Write

Write a structured NumPy array to a CSV file, ensuring proper formatting of different data types.

Click me to see the sample solution


5. CSV with Missing Values Read

Write a NumPy program that reads a CSV file containing missing values into a NumPy array, handling the missing values appropriately.

Click me to see the sample solution


6. Space-delimited Text File Read/Write

Write a NumPy program that saves a NumPy array to a text file with space as the delimiter and then reads it back into a NumPy array.

Click me to see the sample solution


7. Binary File Save/Load

Write a NumPy array to a binary file using np.save and then load it back using np.load.

Click me to see the sample solution


8. Multiple Arrays in Single Binary File

Write a NumPy program that saves multiple NumPy arrays to a single binary file using np.savez and then loads them back using np.load.

Click me to see the sample solution


9. HDF5 File Write/Read

Write a NumPy array to an HDF5 file and then read it back into a NumPy array.

Click me to see the sample solution


10. Specific Dataset from HDF5

Write a NumPy program that reads a specific dataset from an HDF5 file into a NumPy array.

Click me to see the sample solution


11. Compressed .npz File Save/Load

Write a NumPy program that saves a large NumPy array to a compressed .npz file and then reads it back into a NumPy array.

Click me to see the sample solution


12. Excel File Read/Write via Pandas

Write a NumPy array to an Excel file using a library like pandas and then read it back into a NumPy array.

Click me to see the sample solution


13. JSON File Read to Array

Write a NumPy program that reads data from a JSON file into a NumPy array. The JSON file contains numeric data.

Click me to see the sample solution


14. Numeric Array Write to JSON

Write a NumPy array with numeric data to a JSON file.

Click me to see the sample solution


15. CSV to DataFrame to Array Conversion

Write a NumPy program that reads data from a CSV file into a Pandas DataFrame and then converts it to a NumPy array.

Click me to see the sample solution


16. Array to SQLite Database

Write a NumPy array to a SQLite database and then read it back into a NumPy array.

Click me to see the sample solution


17. MATLAB .mat File Round-trip

Write a NumPy program that saves a NumPy array to a MATLAB .mat file using scipy.io.savemat and then reads it back using scipy.io.loadmat.

Click me to see the sample solution


18. Image File Read (PNG)

Write a NumPy program that read image data from a PNG file into a NumPy array using an image processing library like PIL or opencv.

Click me to see the sample solution


19. Save Array as Image File

Write a NumPy program that saves a NumPy array as an image file (e.g., PNG) using an image processing library like PIL or opencv.

Click me to see the sample solution


20. Array Append to List Conversion

Write a NumPy program to append a NumPy array to a Python list and print the result.

Click me to see the sample solution


Python-Numpy Code Editor:

More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.

Test your Python skills with w3resource's quiz



Follow us on Facebook and Twitter for latest update.