从多个.dat文件中读取最后一行并转换为CSV

v7pvogib  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(120)

我有多个.dat文件的数据集,但只需要每个文件的最后一行。到目前为止,我可以循环通过我的文件夹结构,可以读出文件,但只是作为一个整体或不正确的。在csv文件中,数据只是在一个单元格中。
我尝试了以前的问题的解决方案,但我只能解决一个,而不是两个。我仍然没有得到只是最后一行追加。到现在为止,他使x(文件数)在我的csv文件中的条目,但它们既不是分开的,也不是正确的值。值甚至在我的.dat文件中根本没有。
第一列应该包含条目的名称,但当我努力追加最后一行时,我在这里也遇到了问题,我不知道如何在循环通过我的文件夹之前访问这一个。
代码如下所示: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
bvpmtnay

bvpmtnay1#

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

我用这个变体解决了它,如果有人有改进的想法,我愿意接受(还在学习)

相关问题