Member-only story

How To Import XLSX / CSV & TXT Files Into Python!

Manpreet Singh
3 min readJan 15, 2021

--

Python is an amazing language, but you already know that, instead of wasting your time with a background of random stuff let me just show you exactly how to import .xlsx, .csv and even .txt files into Python! I have broken down each one of these files types below so you can skip to whichever one you want to learn about.

XLSX Files

To import .xlsx files into Python you will have to install Pandas. You can use “pip install pandas” or “pip3 install pandas” or any other method you typically use to install packages to your Python enviornment. Next up, let’s import pandas by doing the following:

import pandas as pd

Next up all we have to do is copy this following code:

dataframe = pd.read_excel (r'DIRECTORY TO YOUR XLSX FILE')
print (dataframe)

Let me break down this code, we first initialize the variable where we store the xlsx data (dataframe is the variable). Next we use the Pandas built in function of reading in excel files (read_excel). All you have to do now is get the directory of the xlsx file and replace the text above between the quotes to your specific directory. Here is what the code will look like with the directory pasted:

import pandas as pd

dataframe = pd.read_excel(r'/Users/users/Documents/GitHub/dataapp/GoogleTrends/data/newdata.xlsx')
print (dataframe)

--

--

Manpreet Singh
Manpreet Singh

Responses (1)