将具有二进制数据的多个.dat文件转换为具有相同文件名的.csv

时间:2018-06-18 15:33:49

标签: python pandas csv binary

Error description我有一个带有二进制数据的.dat文件的文件夹,我想解析并写入.csv文件,其名称与.dat文件的名称相同。我可以使用单个.dat文件并将其转换为各自的.csv到我需要的文件夹。

     import numpy as np
     import pandas as pd
     raw_file= '/home/targetfolder/channel1.dat'
     with open(raw_file, "rb") as f:

              raw_data = np.fromstring(f.read(), dtype=np.float32)
              comb_np_array = np.vstack(raw_data)
              big_frame = pd.DataFrame(comb_np_array)
              big_frame.to_csv("/home/destinationfolder/channel1.csv")

     f.close()

'filename.csv中的输出 0,-47.418867 1,-47.443828 2,-47.4445311 3,-47.4738281 4,-47.4193871 5,-47.4222221 6,-47.4193878

以下是我执行相同操作的链接:Python: How to save *.dat-files as *.csv-files to new folder

    import csv
    from os import listdir
    from os.path import isfile, join, splitext

    dat_folder = "/home/nri/"
    csv_folder = "/home/nri/tmp/"

     onlyfilenames = [f for f in listdir(dat_folder) if 
                      isfile(join(dat_folder,f))]
     for fullfilename in onlyfilenames:
          file_name, file_extension = splitext(fullfilename)
          if file_extension == ".dat":
              inputfile=dat_folder + fullfilename
              with open(inputfile, "rb") as f:

                   inputfile = np.fromstring(f.read(), dtype=np.float32)
                   comb_np_array = np.vstack(raw_data)
                   n = pd.DataFrame(comb_np_array)

             with open(join(csv_folder, file_name + ".csv"), "w", 
                            newline='') as f:
                    writer = csv.writer(f,lineterminator='\n')
                    for row in range(len(n)):
                          writer.writerows(n)

但是上面给出了一个错误:“序列预期”。请告诉我如何获得理想的结果。

1 个答案:

答案 0 :(得分:1)

你正在迭代len(n),但每次都写n。

for row in range(len(n)):
    writer.writerows(n)

writerows接受序列序列,但是你传递一个数据帧并期望编写器对它进行迭代,但是数据帧不是你的情况下的序列(python 2?)。 Here is how you can check this

>>> import collections
>>> isinstance(big_frame, collections.Sequence)
False

您需要iterate over the data frame并编写单元格,

for i, row in big_frame.iterrows():
    writer.writerow([i, row[0]]) # 0 is the col name here