使用Python读取.dat文件

问题描述

当前,我正在尝试使用Python提取OPPORTUNITY Activity Recognition Data Set 中的数据。

一个下载链接,因为我无法在Stackoverflow中添加文件。该文件可以在具有复杂结构的Matlab中打开,因此我仍在努力提取文件

但是我要提取数据的文件是S1-ADL1.dat,位于上面链接的“数据集”文件夹中。

以下是文件中的内容File Content

我正在使用代码

with open("S1-ADL1.dat") as infile:
    file_contents = infile.readlines()
print(file_contents)

但是终端什么也没返回。

有人知道如何获取数据吗?请帮帮我。

谢谢。

解决方法

要么与readlines()一起使用for循环,要么使用read()

with open("S1-ADL1.dat") as infile:
    file_contents = infile.read()

print(file_contents)

file = open('S1-ADL1.dat','r') 
file_contents = file.readlines() 

for line in file_contents: 
    print(line.strip())

file.close()