Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Read File As String in Python
Next article icon

How To Read .Data Files In Python?

Last Updated : 01 Feb, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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, the Pandas library, the NumPy library, and a custom approach to cater to different preferences and file structures.

Read Dot Data Files in Python

Below are some of the ways by which we can read .data files in Python:

Reading the .data Text File

In this example, the code opens a .data file in write mode, writes the string "Hello GeeksforGeeks!!!" into it, and then closes the file. Subsequently, it opens the same file in read-only mode, reads its content, and prints it to the console before closing the file.

Python3
# Open the .data file in write mode geeks_data_file = open("geeksforgeeks.data", "w")  # Write data into the file geeks_data_file.write("Hello GeeksforGeeks!!!")  # Close the file geeks_data_file.close()  # Open the .data file in read-only mode geeks_data_file = open("geeksforgeeks.data", "r")  # Read the data of the file and print it print('The content in the file is:')  print(geeks_data_file.read())  # Close the file geeks_data_file.close() 

Output
The content in the file is: Hello GeeksforGeeks!!! 

Reading the .data Binary File

In this example, the code opens a .data file in write-binary mode, encodes and writes the string "Hello GeeksforGeeks!!!" into it, and then closes the file. Subsequently, it opens the same file in read-binary mode, reads its binary data, and prints it to the console before closing the file.

Python3
# Open the .data file in write-binary mode binary_file = open("geeksforgeeks.data", "wb")  # Write data in encoded format into the file binary_file.write("Hello GeeksforGeeks!!!".encode())  # Close the file binary_file.close()  # Open the .data file in read-binary mode binary_file = open("geeksforgeeks.data", "rb")  # Read the data of the binary .data file and print it print('The content in the file is:')  print(binary_file.read()) # Close the file binary_file.close() 

Output
The content in the file is: b'Hello GeeksforGeeks!!!' 

Reading .data Files Using Built-in Python Functions

In this example, the code reads the content of a .data file named 'geeks.data' using the 'with' statement, prints the content to the console, and then automatically closes the file.

geeks.data

Hello, I am a Proud Geeks
Python3
# Reading from a .data file file_path = 'geeks.data'  with open(file_path, 'r') as file:     content = file.read()  print(f"The content of the .data file is:\n{content}") 

Output:

The content of the .data file is:
Hello, I am a Proud Geek

Reading .data Files Using Pandas

In this example, Pandas is employed to read a .data file named 'geeksforgeeks.data.' The code utilizes the `read_csv()` function to load the data into a Pandas DataFrame, adjusting the delimiter as needed based on the file structure, and then prints the DataFrame to the console.

geeksforgeeks.data

Hello, I am a Proud Geeks
Python3
# In this example, we use Pandas to read a .data file. import pandas as pd  file_path = 'geeksforgeeks.data'  # Adjust delimiter based on your file structure df = pd.read_csv(file_path, delimiter='\t') print(df) 

Output:

The content of the .data file is:
Hello, I am a Proud Geek

Numerical Data Extraction Using NumPy

In this example, NumPy is employed to read a .data file named 'geeksforgeeks.data' containing numerical data. The code uses the `loadtxt()` function to load the data into a NumPy array, adjusting the delimiter as needed based on the file structure, and then prints the array to the console.

geeksforgeeks.data

Hello, I am a Proud Geeks
Python3
# In this example, we use NumPy to read a .data file containing numerical data. import numpy as np  file_path = 'geeksforgeeks.data'  # Adjust delimiter based on your file structure data_array = np.loadtxt(file_path, delimiter='\t') print(data_array) 

Output:

The content of the .data file is:
Hello, I am a Proud Geek

Next Article
Read File As String in Python

R

rahulsanketpal0431
Improve
Article Tags :
  • Python
  • Geeks Premier League
  • Geeks Premier League 2023
Practice Tags :
  • python

Similar Reads

  • How to Read from a File in Python
    Reading from a file in Python means accessing and retrieving the contents of a file, whether it be text, binary data or a specific data format like CSV or JSON. Python provides built-in functions and methods for reading a file in python efficiently. Example File: geeks.txt Hello World Hello Geeksfor
    5 min read
  • How to read Dictionary from File in Python?
    A Dictionary in Python is collection of key-value pairs, where key is always unique and oftenly we need to store a dictionary and read it back again. We can read a dictionary from a file in 3 ways: Using the json.loads() method : Converts the string of valid dictionary into json form. Using the ast.
    2 min read
  • How to read large text files in Python?
    In this article, we will try to understand how to read a large text file using the fastest way, with less memory usage using Python.  To read large text files in Python, we can use the file object as an iterator to iterate over the file and perform the required task. Since the iterator just iterates
    3 min read
  • How to read numbers in CSV files in Python?
    Prerequisites: Reading and Writing data in CSV, Creating CSV files  CSV is a Comma-Separated Values file, which allows plain-text data to be saved in a tabular format. These files are stored in our system with a .csv extension. CSV files differ from other spreadsheet file types (like Microsoft Excel
    4 min read
  • Read File As String in Python
    Python provides several ways to read the contents of a file as a string, allowing developers to handle text data with ease. In this article, we will explore four different approaches to achieve this task. Each approach has its advantages and uses cases, so let's delve into them one by one. Read File
    3 min read
  • How to read multiple data files into Pandas?
    In this article, we are going to see how to read multiple data files into pandas, data files are of multiple types, here are a few ways to read multiple files by using the pandas package in python. The demonstrative files can be download from here Method 1: Reading CSV files If our data files are in
    3 min read
  • How to Read Many ASCII Files into R?
    Reading data from ASCII files into R is a common task in data analysis and statistical computing. ASCII files, known for their simplicity and wide compatibility, often contain text data that can be easily processed in R. Here we read multiple ASCII files into R Programming Language. What are ASCII F
    4 min read
  • How to copy file in Python3?
    Prerequisites: Shutil When we require a backup of data, we usually make a copy of that file. Python supports file handling and allows users to handle files i.e., to read and write files, along with many other file handling options, to operate on files. Here we will learn how to copy a file using Pyt
    2 min read
  • How to check file size in Python?
    Prerequisites: os pathlib Given a file, the task here is to generate a Python Script to print its size. This article explains 2 methods to do so. ApproachImport moduleGet file sizeFile in use Name: Data.csv Size: 226 bytes Method1: Using pathlib Path().stat().st_size() function of pathlib module get
    1 min read
  • How to search a pickle file in Python?
    Prerequisites: pickle file  Python pickle module is used for serializing and de-serializing a Python object structure. Any object in Python can be pickled so that it can be saved on disk. What pickle does is that it “serializes” the object first before writing it to file. Pickling is a way to conver
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences