我有一个包含多个 .dat 文件的数据集,但只需要每个文件的最后一行。到目前为止,我可以遍历我的文件夹结构并可以读出文件,但只是作为一个整体或不正确。在 csv 文件中,数据只在一个单元格中。
我尝试了以前问题的解决方案,但我只能解决一个问题,不能同时解决两个问题。我仍然没有得到附加的最后一行。到目前为止,他在我的 csv 文件中创建了 x(文件数)条目,但它们既没有分开也没有正确的值。我的 .dat 文件中根本没有这些值。
第一列应该包含条目的名称,但是当我努力追加最后一行时,我在这里也遇到了问题,我不知道如何在循环遍历我的文件夹之前访问这个。我想将它们全部转换为 csv 文件,但这似乎效率不高。
代码如下: All_Forces.csv 应该是我想要所有数据的 csv 文件,我在循环之前创建它所以它不会被覆盖。
.dat 文件包含 25 个不同的列,由 1000-10000+ 行的空格分隔。
from csv import writer
import pandas as pd
import os.path
df = pd.DataFrame(list())
df.to_csv('All_Forces.csv')
allV = ['V0.8','V1','V1.1']
allD = ['D1','D2','D3']
allPhi = ['Phi1','Phi2','Phi3']
allZ = ['Z1','Z2','Z3']
allT = ['T1','T2','T3']
for V in allV:
for D in allD:
for Phi in allPhi:
for Z in allZ:
for T in allT:
# example path to one file
# D:\V1\D1\Phi1\V1-T1-D1-Phi1-Z1\ForceDat
datname = "D:/" + str(V) + "/" + str(D) + "/" + str(Phi) + "/" + str(V) + "-" + str(T) + "-"+ str(D) + "-"+ str(Phi) + "-"+ str(Z) + "/ForceDat/body_0_forces.dat"
#first row entry should have this name
rowname = str(V) + "_" + str(T) + "_" + str(D) + "_" + str(Phi) + "_" + str(Z)
# not all calculations done, check if file exists
check_file = os.path.isfile(datname)
while check_file == True:
# last line
with open(datname, 'rb') as f:
lines = f.read().splitlines()
last_line = lines[-1]
List1 = last_line
#open our existing csv file in append mode
# create a file object for this file
with open('All_Forces.csv', 'a') as f_object:
writer_object = writer(f_object)
# pass the list as an argument into
# the writerow()
writer_object.writerow(List1)
# Close the file object
f_object.close()
break
import csv
import pandas as pd
import os.path
allD = ['D000','D075', 'D054', 'D068']
allPhi = ['Phi000','Phi300','Phi-300','Phi600','Phi-600']
allZ = ['Z027','Z030','Z032']
allT = ['T-300','T-450','T-600']
outfile = open('All_Forces_V0.8.csv',"a", newline='')
writer = csv.writer(outfile, delimiter =';')
for D in allD:
for Z in allZ:
for Phi in allPhi:
for T in allT:
datname = "D:/V0.8/" + str(T) + "_" + str(Phi) + "_" + str(Z) + "_" + str(D) + "/ForceDat/body_0_forces.dat"
rowname = str(T) + "_" + str(Phi) + "_" + str(Z) + "_" +str(D)
check_file = os.path.isfile(datname)
while check_file == True:
with open(datname, "r") as f:
lines = f.read().splitlines()
last_line = lines[-1]
data = last_line.split()
data = [float(s) for s in data]
if data[0] > 0.5:
data.insert(0, 'True')
else:
data.insert(0, 'false')
data.insert(0, rowname)
writer.writerow(data)
break
我用这个变体解决了它,如果有人有改进的想法我会开放(仍在学习中)