In Python, there are several ways to import a CSV file. The built-in CSV module and the Numpy library are among the most common methods.
Most Python users are familiar with the pandas library, which offers a straightforward and versatile way to import CSV files. However, for various reasons, you might want or need to import a CSV file without using Pandas.
In this tutorial, you will learn how to do just that. Here, we will walk you through how to import a CSV using the built-in CSV module and NumPy.
Using Python’s Built-in CSV module
The Python CSV module which comes bundled with Python, can read and write CSV files and it largely works by reading data into lists and writing data from lists.
Here we have a sample CSV file:
name,age,country John,32,USA Mary,28,Canada Peter,40,UK
The following steps will guide you to load the above CSV file:
Step 1: Import the CSV module
Firstly, you need to import the CSV module using the following command:
1 |
import csv |
Step 2: Open the CSV file
To open the file you can use the open() function and the only parameter you need to pass to it is the name of the CSV file.
1 |
file = open('sample.csv') |
Step 3: Actually import the CSV file
The csv.reader() function is used to read the file.
1 |
csvreader = csv.reader(file) |
Step 4: Convert the CSV to a list of lists
You can convert csvreader to a list and then print it to view it:
1 2 3 4 5 6 7 |
header = [] header = next(csvreader) rows = [] for row in csvreader: rows.append(row) print(header) print(rows) |
Code Output
['name', 'age', 'country'] [['John', '32', 'USA'], ['Mary', '28', 'Canada'], ['Peter', '40', 'UK']]
Using NumPy Library to Import CSV Files
Another way to import a CSV file into Python is to use NumPy library which is suitable for numerical data. Here’s how to do it:
Step 1: Import the NumPy module
1 |
import numpy as np |
Step 2: Read the CSV file
Use the numpy.genfromtxt() function to read the CSV file:
1 |
data = np.genfromtxt('sample.csv', delimiter=',', names=True, dtype=None, encoding='UTF') |
Step 3: Print the final data
Finally, print all of your data to make sure it’s coming out correctly:
1 |
print(data) |
Final Code
The full Python code for both options:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import csv import numpy as np #using csv module file = open('sample.csv') csvreader = csv.reader(file) header = [] header = next(csvreader) rows = [] for row in csvreader: rows.append(row) print(header) print(rows) #using numpy data = np.genfromtxt('sample.csv', delimiter=',', names=True, dtype=None, encoding='UTF') print(data) |
Conclusion
In this tutorial, we learned the ropes of importing a CSV file into Python without resorting to the Pandas library. Leveraging Python’s built-in CSV module or the NumPy library, we can effectively process our CSV data in various ways to suit our programming needs. Happy coding!