Open In App

How To Read .Data Files In Python?

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads