Open In App

Loading Different Data Files in Python

Last Updated : 15 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

nn

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

excel

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:

Screenshot-(240)

Roa-Haru.mp3


Previous Article
Next Article

Similar Reads

How to merge multiple excel files into a single files with Python ?
Normally, we're working with Excel files, and we surely have come across a scenario where we need to merge multiple Excel files into one. The traditional method has always been using a VBA code inside excel which does the job but is a multi-step process and is not so easy to understand. Another method is manually copying long Excel files into one w
4 min read
Loading Data in Pytorch
In this article, we will discuss how to load different kinds of data in PyTorch. For demonstration purposes, Pytorch comes with 3 divisions of datasets namely torchaudio, torchvision, and torchtext. We can leverage these demo datasets to understand how to load Sound, Image, and text data using Pytorch. Torchaudio Dataset Loading demo yes_no audio d
5 min read
Loading Image using Mahotas - Python
In this article we will see can load image in mahotas. Mahotas is a computer vision and image processing and manipulation library for Python. A library is a collection of functions and methods that allows you to perform many actions without having to write hundreds of lines of code. Mahotas includes many algorithms that operates with arrays, mahota
1 min read
How to Check Loading Time of Website using Python
In this article, we will discuss how we can check the website's loading time. Do you want to calculate how much time it will take to load your website? Then, what you must need to exactly do is subtract the time obtained passed since the epoch from the time obtained after reading the whole website. Stepwise Implementation:Step 1: Import the librari
3 min read
How to apply different titles for each different subplots using Plotly in Python?
Prerequisites: Python Plotly In this article, we will explore how to apply different titles for each different subplot. One of the most deceptively-powerful features of data visualization is the ability for a viewer to quickly analyze a sufficient amount of information about data when pointing the cursor over the point label appears. It provides us
2 min read
Python | Write multiple files data to master file
Given a number of input files in a source directory, write a Python program to read data from all the files and write it to a single master file. Source directory contains n number of files, and structure is same for all files. The objective of this code is to read all the files one by one and then append the output into a single master file having
2 min read
How to Scrape Data From Local HTML Files using Python?
BeautifulSoup module in Python allows us to scrape data from local HTML files. For some reason, website pages might get stored in a local (offline environment), and whenever in need, there may be requirements to get the data from them. Sometimes there may be a need to get data from multiple Locally stored HTML files too. Usually HTML files got the
4 min read
Joining Excel Data from Multiple files using Python Pandas
Let us see how to join the data of two excel files and save the merged data as a new Excel file. We have 2 files, registration details.xlsx and exam results.xlsx. registration details.xlsx We are having 7 columns in this file with 14 unique students details. Column names are as follows : Admission Date Name of Student Gender DOB Student email Id En
2 min read
Extract Data from PGN Files Using the Chess Library in Python
In this article, we are going to see how to extract data from PGN Files using the chess library in Python. With the help of the chess library in python, we can perform several operations like validating a move, extracting data, and even making moves on the chessboard. In this article, we will extract data from a PGN file or a PGN string using the p
6 min read
How To Read .Data Files In Python?
Unlocking the secrets of reading .data files in Python involves navigating through diverse structures. In this article, we will unravel the mysteries of reading .data files in Python through four distinct approaches. Understanding the structure of .data files is essential, as their format may vary widely. We'll explore built-in Python functions, th
4 min read
Article Tags :
Practice Tags :