卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16584本站已运行3318

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

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

在本文中,我们将学习什么是 .data 文件以及如何在 Python 中读取 .data 文件。

什么是 .data 文件?

.data 文件是为了存储信息/数据而创建的。

这种格式的数据经常以逗号分隔值格式或制表符分隔值格式放置。

除此之外,该文件可能是二进制或文本文件格式。在这种情况下,我们必须找到另一种方法来访问它。

在本教程中,我们将使用.csv 文件,但首先,我们必须确定文件的内容是文本还是二进制。

在 .data 文件中识别数据

.data 文件有两种格式,文件本身可以是文本或二进制。

我们需要加载并自行测试,以确定它属于哪一个。

读取 .data 文本文件

.data文件通常是文本文件,使用Python读取文件很简单。

由于文件处理是作为 Python 的一项功能预先构建的,因此我们不需要导入任何模块即可使用它。

话虽如此,以下是如何在 Python 中打开、读取和写入文件 -

算法(步骤)

以下是执行所需任务所需遵循的算法/步骤。 -

  • 再次使用open()函数以写入模式打开.data文件,通过将文件名和模式'w'作为参数传递给它。如果指定的文件不存在,则会创建一个具有给定名称的文件并以写入模式打开。

  • 使用write()函数将一些随机数据写入文件。

  • 将数据写入文件后,使用close()函数关闭文件。

  • 使用open()函数(打开一个文件并返回一个文件对象作为结果)通过传递文件名和模式'r'作为参数来以只读模式打开.data文件。

  • 使用read()函数(从文件中读取指定数量的字节并返回,默认值为-1,表示整个文件)读取数据文件的。并打印出来

  • 使用close()函数在从文件中读取数据后关闭文件。

示例

以下程序展示了如何在Python中读取一个文本 .data 文件 −

# 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()

输出

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

读取.data二进制文件

.data 文件也可以是二进制文件的形式。这意味着我们必须更改访问文件的方法。

我们将以二进制模式读写文件;在这种情况下,模式是rb,即读取二进制。

话虽如此,以下是在Python中打开、读取和写入文件的方法:

算法(步骤)

以下是执行所需任务所需遵循的算法/步骤。 -

  • 再次使用open()函数以写入二进制模式打开.data文件,通过将相同的文件名和模式'wb'作为参数传递给它。如果指定的文件不存在,则会创建一个具有给定名称的文件,并以写入二进制模式打开。

  • 当我们将数据写入二进制文件时,我们必须将数据从文本格式转换为二进制格式,这可以通过encode()函数来实现(在Python中,encode()方法负责返回任何提供的文本的编码形式。为了有效地存储这些字符串,代码点被转换为一系列字节。这被称为编码。Python的默认编码是utf-8)。

  • 使用write()函数将上述编码数据写入文件。

  • 将二进制数据写入文件后,使用close()函数关闭文件。

  • 使用open()函数(打开文件并返回文件对象作为结果)通过将文件名和模式'rb'作为参数传递给它来以读取二进制模式打开.data文件。

  • 使用 read() 函数(从文件中读取指定数量的字节并返回它们。默认值为-1,表示整个文件)读取文件的数据并打印。

  • 从文件中读取二进制数据后,使用close()函数关闭文件。

示例

以下程序展示了如何在Python中读取二进制的.data文件 −

# 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()

输出

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

Python 中的文件操作相当简单易懂,如果您想了解各种文件访问模式和方法,值得探索。

任何一种方法都应该可以工作,并为您提供一种获取 .data 文件内容信息的方法。

既然我们知道了 CSV 文件的格式,我们就可以使用 pandas 为它创建一个 DataFrame。

结论

在本文中,我们了解了 .data 文件是什么以及 .data 文件中可以保存哪些类型的数据。使用 open() 和 read() 函数,我们学习了如何读取多种类型的 .data 文件,例如文本文件和二进制文件。我们还学习了如何使用encode()函数将字符串转换为字节。

卓越飞翔博客
上一篇: Python程序将两个整数值连接成一个
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏