将.DAT转换为CSV时,我的代码会更改输出值



我有一个.dat文件,我正在尝试将其转换为CSV。我发现了一块"某种程度上"解决我的问题的代码。问题是:结果给了我一个混乱的输出文件。换句话说:它改变了我的价值观!!!!

有人可以帮助我吗?我是这个总体的。

非常感谢。

with open('f.dat') as input_file:
    lines = input_file.readlines()
    newLines = []
    for line in lines:
        newLines.append(newLine)
with open('f_out.csv','w') as output_file:
    file_writer = csv.writer(output_file)
    file_writer.writerows(newLines)

我的输入文件看起来像这样:

"-18.7723311308 3166157043.25795    0   1006743187.3562
-18.8214122765  188717303.231381    0   57141624.5127759
-18.7022205742  399933910.540253    0   87142384.8698447
-18.5903166748  23045528.3797531    0   5841919.83133624
-18.3051499783  76457482.0309581    0   25326122.2381197"
(with more lines)

和这样的输出文件:

-21.5607314306,1200000000.0,0,500000000.0,MBH
-21.5607314306,1200000000.0,0,500000000.0,MBH
-21.5607314306,1200000000.0,0,500000000.0,MBH

我只是想要的是一个输出文件,其中我的列是由逗号分隔的,例如:

"-18.7723311308, 3166157043.25795, 0, 1006743187.3562
-18.8214122765, 188717303.231381, 0 ,57141624.5127759"

.dat文件使用文件IO操作不太可读您可以使用ASAMMDF模块读取.dat文件。PIP安装ASAMMDF

有一个称为pandas的模块,可以轻松将列表转换为CSV文件。尝试使用此代码。

import pandas as pd
with open('f.dat') as input_file:
    lines = input_file.readlines()
    newLines = []
    for line in lines:
        newLines.append(line)
pd.DataFrame(newLines).to_csv('f_out.csv')

.dat文件使用文件IO操作不太可以读取,您可以使用ASAMMDF模块读取.dat文件。使用PIP install asammdf

.dat文件在 asammdf 的上下文中是测量数据文件V2或V3,通常在汽车域中使用。我认为OP没有这种文件

最新更新