How to Import a CSV File in Python Without Using Pandas?

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:

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.

Step 3: Actually import the CSV file

The csv.reader() function is used to read the 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:

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

Step 2: Read the CSV file

Use the numpy.genfromtxt() function to read the CSV file:

Step 3: Print the final data

Finally, print all of your data to make sure it’s coming out correctly:

Final Code

The full Python code for both options:

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!