简体   繁体   中英

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?

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)
writer = pd.ExcelWriter('pandas_example.dat',
                        engine='xlsxwriter',
                        options={'strings_to_urls': False})

or you can use :

pd.to_excel('example.xlsx')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM