How to Convert .Dat to .Csv Using Python

How to convert .dat to .csv using python?

str.strip() removes leading and trailing characters from a string.

You want to split the lines on "|", then strip each element of the resulting list:

import csv

with open('file.dat') as dat_file, open('file.csv', 'w') as csv_file:
csv_writer = csv.writer(csv_file)

for line in dat_file:
row = [field.strip() for field in line.split('|')]
if len(row) == 6 and row[3] and row[4]:
csv_writer.writerow(row)

Problem in converting .dat file into csv format using pandas, Need to split 1 column into multiple columns

.dat is creating confusion here, in this case, it's a file with space delimited.
so just put the separator = " "

import pandas as pd
df=pd.read_csv("KY1801.32m.dat",sep= " ")

Sample Image

convert .dat into .csv in python

If all your rows follow that consistent format, you can use pd.read_fwf. This is a little safer than using read_csv, in the event that your second column also contains the delimiter you are attempting to split on.

df = pd.read_fwf('data.txt', header=None, 
widths=[2, int(1e5)], names=['label', 'text'])

print(df)
label text
0 -1 ieafxf rjzy xfxk ymi wuy
1 1 lqqm ceegjnbjpxnidygr
2 -1 zss awoj anxb rfw kgbvnl

data.txt

-1  ieafxf  rjzy xfxk ymi wuy
+1 lqqm ceegjnbjpxnidygr
-1 zss awoj anxb rfw kgbvnl

Converting mixed-format .DAT to .CSV (or anything else)

Treat those header lines in the input file with all the disdain they deserve. (Or, in other words, read them and discard them.)

headers='Year Month Day Hour Minute Direct Diffuse2 D_Global D_IR U_Global U_IR Zenith'
with open ( 'temp.dat') as input_file:
with open ('temp_2.csv', 'w') as output_file:
output_file.write('"%s"\n'%'","'.join(headers.split()))
for count, line in enumerate(input_file):
if count<4: continue
outLine = ','.join(line.split())
output_file.write(outLine + '\n')

Python: convert DAT files to XLS

I'd use pandas.

import pandas as pd
df = pd.read_table('DATA.DAT')
df.to_excel('DATA.xlsx')

and of course you can setup a loop to get through all you files. Something along these lines maybe

import glob
import os
os.chdir("C:\\FILEPATH\\")
for file in glob.glob("*.DAT"):
#What file is being converted
print file
df = pd.read_table(file)
file1 = file.replace('DAT','xlsx')
df.to_excel(file1)


Related Topics



Leave a reply



Submit