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= " ")
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
How to Install Pypdf2 Module Using Windows
Converting Two Lists into a Matrix
Retrieving Subfolders Names in S3 Bucket from Boto3
What Is the Most Pythonic Way to Check If Multiple Variables Are Not None
Python Comparing List Values to Keys in List of Dicts
Django Development Server, How to Stop It When It Run in Background
Import Local Module in Jupyter Notebook
Calculate Angle (Clockwise) Between Two Points
How to Run an .Ipynb Jupyter Notebook from Terminal
How to Make a Discord Bot Leave a Server from a Command in Another Server
How to Write to an Existing Excel File Without Overwriting Data (Using Pandas)
Python 3D Polynomial Surface Fit, Order Dependent
How to Convert Float into Hours Minutes Seconds
How to Change Border Color in Tkinter Widget
How to Merge Two Cnn That Are Trained Over Different Data Stream
Removing Punctuations and Spaces in a String Without Using Regex
How to Crop the Black Background of the Image Using Opencv in Python