How do I read a .data file in Python?


In this article, we will learn what is a .data file and how to read a .data file in python.

What is a .data file?

.data files were created to store information/data.

Data in this format is frequently placed in either a comma-separated value format or a tab-separated value format.

In addition to that, the file may be in binary or text file format. In that case, we'll have to find another way to access it.

For this tutorial, we will be working with.csv files, but first, we must determine whether the file's content is text or binary.

Identifying data within .data files

.data files are available in two formats, with the file itself being either text or binary.

We'll have to load it up and test it ourselves to figure out which one it belongs to.

Reading the .data Text File

.data files are generally text files, and reading files with Python is simple.

Because file handling is pre-built as a feature of Python, we don't need to import any modules to work with it.

That being said, the following is how you open, read, and write to a file in Python −

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Use the open() function again to open the .data file in write mode by passing the file name, and mode ‘w’ as arguments to it. If the file specified does not exist, it creates one with the given name and opens it in writing mode.

  • Write some random data into the file using the write() function.

  • Use the close() function to close the file after writing data into the file.

  • Use the open() function(opens a file and returns a file object as a result) to open the .data file in read-only mode by passing the file name, and mode ‘r’ as arguments to it.

  • Use the read() function(reads the specified number of bytes from the file and returns them. The default value is -1, which means the entire file) to read the data of the file. and print it

  • Use the close() function to close the file after reading data from the file.

Example

The following program shows how to read a text .data file in Python −

# opening the .data file in write mode
datafile = open("tutorialspoint.data", "w")
# writing data into the file
datafile.write("Hello Everyone this is tutorialsPoint!!!")
# closing the file
datafile.close()
 
# opening the .data file in read-only mode 
datafile = open("tutorialspoint.data", "r")
# reading the data of the file and printing it
print('The content in the file is:')
print(datafile.read())
# closing the file
datafile.close()

Output

The content in the file is:
Hello Everyone this is tutorialsPoint!!!

Reading the .data Binary File

The .data files may also be in the form of binary files. This implies that we must change the method we access the file.

We'll read and write to the file in binary mode; in this case, the mode is rb, or read binary.

That being said, the following is how you open, read, and write a file in Python −

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task. −

  • Use the open() function again to open the .data file in write-binary mode by passing the same file name and mode ‘wb’ as arguments to it. If the file specified does not exist, it creates one with the given name and opens it in write binary mode.

  • As we write to the binary file, we must transform the data from text to binary format, which we can do with the encode() function(The encode() method in Python is responsible for returning the encoded form of any supplied text. To efficiently store such strings, the code points are converted into a series of bytes. This is known as encoding. Python's default encoding is utf-8).

  • Write the above-encoded data into the file using the write() function.

  • Use the close() function to close the file after writing the binary data into the file.

  • Use the open() function(opens a file and returns a file object as a result) to open the .data file in read-binary mode by passing the file name, and mode ‘rb’ as arguments to it.

  • Use the read() function(reads the specified number of bytes from the file and returns them. The default value is -1, which means the entire file) to read the data of the file and print it.

  • Use the close() function to close the file after reading the binary data from the file.

Example

The following program shows how to read a binary .data file in Python −

# opening the .data file in write-binary mode
datafile = open("tutorialspoint.data", "wb")
# writing data in encoded format into the file
datafile.write("Hello Everyone this is tutorialspoint!!!".encode())
# closing the file
datafile.close()

# opening the .data file in read-binary mode 
datafile = open("tutorialspoint.data", "rb")
# reading the data of the binary .data file and printing it
print('The content in the file is:')
print(datafile.read())
# closing the file
datafile.close()

Output

The content in the file is:
b'Hello Everyone this is tutorialspoint!!!'

File operations in Python are reasonably simple to understand and are worth exploring if you want to learn about the various file access modes and methods.

Either of these ways should work and offer you a method to get information about the contents of the .data file.

We can use pandas to create a DataFrame for the CSV file now that we know what format it is in.

Conclusion

In this article, we learned what a.data file is and what types of data can be saved in .data files. Using the open() and read() functions, we learned how to read several types of .data files, such as text files and binary files. We also learned how to use the encode() function to convert a string to Bytes.

Updated on: 31-Jan-2023

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements