Loading Different Data Files in Python
Last Updated :
15 Apr, 2024
We are given different Data files and our task is to load all of them using Python. In this article, we will discuss how to load different data files in Python.
Loading Different Data Files in Python
Below, are the example of Loading Different Data Files in Python:
- Loading Plain Text Files
- Loading Images
- Loading Excel Files
- Loading Audio Files
Loading Plain Text Files in Python
In this example, the below code shows how to load plain text files in Python. First, it opens the file in read mode ensures proper closing, and then reads the entire file content into a variable named 'data'.
example.txt
GeeksforGeeks is a Coding Platform
Python3
with open('example.txt', 'r') as f:
data = f.read()
print(data)
Output:
GeeksforGeeks is a Coding Platform
Loading Images in Python
In this example, below code loads and displays an image using OpenCV and Matplotlib. It first imports libraries and attempts to read the image "download3.png". An 'if statement' checks for successful loading. If successful, Matplotlib displays the image with a large figure size and hidden axes.
Python3
import matplotlib.pyplot as plt
import cv2
try:
image = cv2.imread('download3.png')
if image is not None:
plt.figure(figsize=(18, 18))
plt.imshow(image)
plt.show()
else:
print("Error! Something went wrong!")
except Exception as e:
print("OOps! Error!", e)
Output:

Loading Excel Files in Python
In this example, below code loads a retail sales dataset from an Excel file. It defines the file path and imports the pandas library for data manipulation. The core functionality lies in data = pd.read_excel(file_path), which reads the Excel data into a pandas DataFrame named data.
excel.xlsx

Python3
import pandas as pd
file_path = 'excel.xlsx'
data = pd.read_excel(file_path)
print(data.head())
Output:
A B
0 12 13
1 14 15
2 16 17
Loading Audio Files in Python
In this example, below code uses librosa, a powerful library for audio analysis. It starts by loading an audio file and the loaded audio data is stored in 'audio' (represented as a NumPy array), while 'sampling_rate' captures the frequency at which the audio was recorded (samples per second).
Python3
import librosa
file_path = "Roa-Haru.mp3"
audio, sampling_rate = librosa.load(file_path)
# display data
print(f"Audio data: {audio.shape}")
print(f"Sampling rate: {sampling_rate} Hz")
Output:
Roa-Haru.mp3
Similar Reads
Python - List Files in a Directory Sometimes, while working with files in Python, a problem arises with how to get all files in a directory. In this article, we will cover different methods of how to list all file names in a directory in Python.Table of ContentWhat is a Directory in Python?How to List Files in a Directory in PythonLi
8 min read
Listing out directories and files in Python The following is a list of some of the important methods/functions in Python with descriptions that you should know to understand this article. len() - It is used to count number of elements(items/characters) of iterables like list, tuple, string, dictionary etc. str() - It is used to transform data
6 min read
How to delete data from file in Python When data is no longer needed, itâs important to free up space for more relevant information. Python's file handling capabilities allow us to manage files easily, whether it's deleting entire files, clearing contents or removing specific data.For more on file handling, check out:File Handling in Pyt
3 min read
Open a File in Python Python provides built-in functions for creating, writing, and reading files. Two types of files can be handled in Python, normal text files and binary files (written in binary language, 0s, and 1s). Text files: In this type of file, each line of text is terminated with a special character called EOL
6 min read
Difference Between json.load() and json.loads() - Python This article discusses the differences between two important methods: json.load() and json.loads(). Both are used to convert JSON data into Python objects, but they are used in different contexts.json.load()json.load() takes a file object and returns the json object. It is used to read JSON encoded
2 min read
PYGLET â Loading a File In this article we will see how we can load a file in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating
2 min read