简体   繁体   English

Python:将DAT文件转换为XLS

[英]Python: convert DAT files to XLS

I have a bunch of DAT files that I need to convert to XLS files using Python. 我有一堆DAT文件,我需要使用Python将其转换为XLS文件。 Should I use the CSV library to do this or is there a better way? 我应该使用CSV库执行此操作还是有更好的方法?

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')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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