我做了一些谷歌搜索,遇到了下面的脚本,将DAT文件转换为CSV文件。
import csv
import idlsave
from os import listdir
from os.path import isfile, join, splitext
dat_folder = "C:\\Users\\ryans\\OneDrive\\Desktop\\dat\\"
csv_folder = "C:\\Users\\ryans\\OneDrive\\Desktop\\csv\\"
onlyfilenames = [f for f in listdir(dat_folder) if isfile(join(dat_folder,f))]
for fullfilename in onlyfilenames:
file_name, file_extension = splitext(fullfilename)
if file_extension == ".DAT":
input_file = idlsave.read(dat_folder + fullfilename)
n = input_file["raw"]
with open(join(csv_folder, file_name + ".CSV"), "w", newline='') as f:
writer = csv.writer(f)
writer.writerows(n)
该代码看起来可以正常工作,并且逻辑看起来不错,但是当我运行它时,我收到以下错误消息:
Exception: Invalid SIGNATURE: b'10'
所有文件名和扩展名均大写。不知道这有什么区别。我搜索了错误,但没有提出任何有用的建议。
答案 0 :(得分:0)
感谢Anil_M,这很有帮助!这是我的最终解决方案。
import csv
import idlsave
from os import listdir
from os.path import isfile, join, splitext
dat_folder = "C:\\Users\\ryans\\OneDrive\\Desktop\\dat\\"
csv_folder = "C:\\Users\\ryans\\OneDrive\\Desktop\\csv\\"
onlyfilenames = [f for f in listdir(dat_folder) if isfile(join(dat_folder,f))]
for fullfilename in onlyfilenames:
file_name, file_extension = splitext(fullfilename)
if file_extension == ".DAT":
with open(join(dat_folder + fullfilename)) as f:
with open(join(csv_folder, file_name + ".CSV"), "w") as f1:
for line in f:
f1.write(line)