I have a folder with .dat files with binary data which I want to parse and write to .csv files with same name as that of .dat files. I could take single .dat file and convert it to its respective .csv to the folder I need.
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()
Output in ´filename.csv 0,-47.418867 1,-47.443828 2,-47.4445311 3,-47.4738281 4,-47.4193871 5,-47.4222221 6,-47.4193878
Here is the link I followed to do the same : 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)
But the above gives an error : "sequence expected" . Please let me know how to desired result.