Home  >  Article  >  Backend Development  >  How to read a .data file in Python?

How to read a .data file in Python?

WBOY
WBOYforward
2023-08-28 21:41:021684browse

如何在Python中读取一个 .data 文件?

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

What is a .data file?

.data files are created to store information/data.

Data in this format is often placed in comma-separated value format or tab-separated value format.

Otherwise, the file may be in binary or text file format. In this case we have to find another way to access it.

In this tutorial we will be using a .csv file, but first we must determine whether the contents of the file are text or binary.

Identifying data in .data files

.data files come in two formats, the file itself can be text or binary.

We need to load it and test it ourselves to determine which one it belongs to.

Read .data text file

.data files are usually text files, and reading them is simple using Python.

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

With that said, here’s how to open, read, and write files in Python -

Algorithm (steps)

Below are the algorithms/steps that need to be followed to perform the required task. -

  • Use the open() function again to open the .data file in write mode by passing it the file name and mode 'w' as arguments. If the specified file does not exist, a file with the given name is created and opened in writing mode.

  • Use the write() function to write some random data to the file.

  • After writing the data to the file, use the close() function to close the file.

  • Use the open() function (which opens a file and returns a file object as a result) to open a .data file in read-only mode by passing the filename and mode 'r' as arguments.

  • Use the read() function (read the specified number of bytes from the file and return it, the default value is -1, indicating the entire file) to read the data 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!!!

Read .data binary file

.data file can also be in the form of a binary file. This means we have to change the way we access the files.

We will read and write the file in binary mode; in this case, the mode is rb, which means read binary.

Having said that, here’s how to open, read, and write files in Python:

Algorithm (steps)

Below are the algorithms/steps that need to be followed to perform the required task. -

  • Use the open() function again to open the .data file in write binary mode by passing it the same filename and mode 'wb' as arguments. If the specified file does not exist, a file with the given name is created and opened in binary mode for writing.

  • When we write data to a binary file, we must convert the data from text format to binary format, which can be achieved through the encode() function (in Python, encode( ) method is responsible for returning the encoded form of any supplied text. To store these strings efficiently, the code points are converted into a sequence of bytes. This is called the encoding. Python's default encoding is UTF-8).

  • Use the write() function to write the above encoded data to the file.

  • After writing the binary data to the file, use the close() function to close the file.

  • Use the open() function (which opens a file and returns a file object as a result) to open a .data file in read binary mode by passing it the filename and mode 'rb' as arguments .

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

  • After reading binary data from the file, use the close() function to close the file.

Example

The following program shows how to read binary .data files 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 fairly simple and easy to understand, and worth exploring if you want to understand the various file access modes and methods.

Either method should work and provide you with a way to get information about the contents of the .data file.

Now that we know the format of the CSV file, we can use pandas to create a DataFrame for it.

in conclusion

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

The above is the detailed content of How to read a .data file in Python?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete