Lesson 28 · Python for Data Science
06 Reading CSV, Excel, and JSON Files in Python
Many real-world data files are in CSV, Excel, or JSON formats. Python makes it easy to get information from these files, so you can analyze or use it. Let…
- CoursePython for Data Science
- Lesson28 of 38
- Video13 min
- FormatJupyter notebook · 20 code cells
What you'll learn
Data
No separate download needed — the notebook creates or downloads everything it uses.
📓 Full notebook
Download .ipynb
Welcome! Let us learn how to read CSV, Excel, and JSON files in Python.#
Many real-world data files are in CSV, Excel, or JSON formats.
Python makes it easy to get information from these files, so you can analyze or use it.
Let us see how to work with each type step by step.
Why learn to read CSV, Excel, and JSON?#
Companies, apps, and even simple lists use these files to store information.
It is a key skill for jobs in data, research, and programming.
Let us start with the very basics first.
# First, let us check our Python version.
import sys
print('Python version:', sys.version)
What do CSV, Excel, and JSON mean?#
- CSV (Comma-Separated Values) is a text file where each line is a row and values are separated by commas.
- Excel files are spreadsheets, like those made in Microsoft Excel. Typical extensions are
.xlsor.xlsx. - JSON (JavaScript Object Notation) stores data in a readable format, often used by web apps.
Let us start with CSV files!
# Let us make a sample CSV file right in Python.
with open('sample.csv', 'w') as f:
f.write('name,age,city\n')
f.write('Anna,23,New York\n')
f.write('Ben,31,London\n')
f.write('Clara,27,Paris\n')
# Now we have a sample.csv file to try out.
# Reading a CSV file using Python's built-in csv module
import csv
with open('sample.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
# Reading the CSV file as dictionaries (easier to access by column name)
with open('sample.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
print(row['name'], row['city'])
# Handling missing values in CSV files
with open('sample.csv', 'r') as file:
reader = csv.DictReader(file)
for row in reader:
name = row.get('name', 'Unknown')
age = row.get('age', 'N/A')
city = row.get('city', 'Somewhere')
print(f'{name} is {age} years old and lives in {city}.')
# Let us read a CSV file using pandas, a popular data library.
import pandas as pd
df = pd.read_csv('sample.csv')
print(df)
# What if the column names are missing or wrong? Let us set them ourselves.
columns = ['Person', 'Years', 'Location']
df2 = pd.read_csv('sample.csv', names=columns, header=0)
print(df2)
# Let us load only certain columns from a bigger CSV file.
columns_to_use = ['name', 'city']
df_small = pd.read_csv('sample.csv', usecols=columns_to_use)
print(df_small)
# Let us save the table we made in pandas to a new Excel file.
df.to_excel('sample_data.xlsx', index=False)
print('Excel file saved!')
# Let us read our new Excel file with pandas.
excel_df = pd.read_excel('sample_data.xlsx')
print(excel_df)
# What if your Excel file has more than one sheet?
sheetnames = pd.ExcelFile('sample_data.xlsx').sheet_names
print('Sheets in this file:', sheetnames)
multi_sheet_df = pd.read_excel('sample_data.xlsx', sheet_name=sheetnames[0])
print(multi_sheet_df)
# Let us make a small example JSON file.
import json
data = [
{'name':'Anna', 'age':23, 'pets':['cat','dog']},
{'name':'Ben', 'age':31, 'pets':[]},
{'name':'Clara', 'age':27, 'pets':['parrot']},
]
with open('sample.json', 'w') as f:
json.dump(data, f)
# Let us read our JSON file back into Python.
with open('sample.json', 'r') as f:
info = json.load(f)
for person in info:
print(person['name'], 'has pets:', person['pets'])
# pandas can also load JSON as a table
json_df = pd.read_json('sample.json')
print(json_df)
# What if your JSON file has nested dictionaries? Let us practice safe access.
data_nested = [
{'id':101, 'details':{'name':'Dana', 'age':22}},
{'id':102, 'details':{'name':'Eli'}},
{'id':103, 'details':{}}
]
with open('nested.json', 'w') as f:
json.dump(data_nested, f)
with open('nested.json') as f:
nested = json.load(f)
for row in nested:
name = row.get('details', {}).get('name', 'Unknown')
age = row.get('details', {}).get('age', 'N/A')
print(f'ID {row['id']}: {name} ({age})')
# Mini-project: Ask the user for a file type then read and print the data.
ftype = input('Which file type do you want to open? (csv, excel, json): ').strip().lower()
result = None
if ftype == 'csv':
result = pd.read_csv('sample.csv')
elif ftype == 'excel':
result = pd.read_excel('sample_data.xlsx')
elif ftype == 'json':
result = pd.read_json('sample.json')
else:
print('Unknown file type. Please enter csv, excel, or json.')
if result is not None:
print(result)
# Extra tip: How to see the first few rows of big data.
df = pd.read_csv('sample.csv')
print(df.head(2))
# Troubleshooting: What if you get a FileNotFoundError?
try:
pd.read_csv('missing.csv')
except FileNotFoundError:
print('Could not find that file. Check the file name or path.')
Recap: You now know how to read CSV, Excel, and JSON files in Python!#
- Practice loading different file types and handling missing data.
- Use pandas for quick, powerful table work.
- Try reading files with multiple sheets or nested data.
- Handle file errors gently with try-except.
With these skills, you can work with almost any data in Python.
Thanks for learning with us!#
Like this video, subscribe for more lessons, and share with friends if you found it useful!
Leave a comment about which file type you plan to use.
Happy coding and see you in the next video!
Found this useful?
All lessons, notebooks and datasets here are free. If they helped you, a coffee keeps new lessons coming.



