Everett
Everett

Reputation: 145

Python: convert DAT files to XLS

I have a bunch of DAT files that I need to convert to XLS files using Python. Should I use the CSV library to do this or is there a better way?

Upvotes: 0

Views: 6061

Answers (2)

Tim Gottgetreu
Tim Gottgetreu

Reputation: 495

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)

Upvotes: 1

Neha
Neha

Reputation: 3660

writer = pd.ExcelWriter('pandas_example.dat',
                        engine='xlsxwriter',
                        options={'strings_to_urls': False})

or you can use :

pd.to_excel('example.xlsx')

Upvotes: 0

Related Questions