Loading Different Data Files in Python Last Updated : 15 Apr, 2024 Comments Improve Suggest changes Like Article Like 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 PythonBelow, 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 PythonIn 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 PlatformLoading Images in PythonIn 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 PythonIn 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 17Loading Audio Files in PythonIn 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 Comment More infoAdvertise with us Next Article Loading Different Data Files in Python audrija19 Follow Improve Article Tags : Python Python-excel Practice Tags : python 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 Python - Difference Between json.load() and json.loads() JSON (JavaScript Object Notation) is a script (executable) file which is made of text in a programming language, is used to store and transfer the data. It is a language-independent format and is very easy to understand since it is self-describing in nature. Python has a built-in package called json 3 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 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 w 4 min read json.load() in Python The full-form of JSON is JavaScript Object Notation. It means that a script (executable) file which is made of text in a programming language, is used to store and transfer the data. Python supports JSON through a built-in package called json. To use this feature, we import the json package in Pytho 3 min read File Mode in Python In Python, the file mode specifies the purpose and the operations that can be performed on a file when it is opened. When you open a file using the open() function, you can specify the file mode as the second argument. Different File Mode in PythonBelow are the different types of file modes in Pytho 5 min read Different ways to import csv file in Pandas CSV files are the "comma separated values", these values are separated by commas, this file can be viewed as an Excel file. In Python, Pandas is the most important library coming to data science. We need to deal with huge datasets while analyzing the data, which usually can be in CSV file format. Le 3 min read Like