在测绘行业工作的朋友们都知道,数据处理往往占据了我们大部分的时间。面对大量的DAT文件需要转换成XYZ格式时,手动操作不仅耗时而且容易出错,我的解决之道就是——“懒”。没错,正是这份“懒”才动手编写了一个Python脚本,有了转换软件我更懒了。
软件界面 如此简单 还是懒得缘故

开源是好习惯代码在此
import tkinter as tk
from tkinter import filedialog, messagebox
def convert_dat_to_xyz(dat_content):
lines = dat_content.strip().split('\n')
converted_lines = [
f"{parts[2].strip()} {parts[3].strip()} {parts[4].strip()}"
for line in lines
if (parts := line.split(',')) and len(parts) == 5
]
return '\n'.join(converted_lines), len(converted_lines)
def convert_xyz_to_dat_combined(xyz_content):
lines = xyz_content.strip().split('\n')
converted_lines = [
f"{i+1},,{parts[0].strip()} ,{parts[1].strip()} ,{parts[2].strip()}"
for i, line in enumerate(lines)
if (parts := line.split()) and len(parts) == 3 or (parts := line.split(',')) and len(parts) == 3
]
return '\n'.join(converted_lines), len(converted_lines)
def open_file(file_type):
file_path = filedialog.askopenfilename(filetypes=[(f"{file_type.upper()} files", f"*.{file_type}")])
if not file_path:
return None
with open(file_path, 'r', encoding='utf-8') as file:
return file.read()
def save_file(content, extension):
file_path = filedialog.asksaveasfilename(defaultextension=extension, filetypes=[(f"{extension.upper()} files", f"*.{extension}"), ("All files", "*.*")])
if file_path:
with open(file_path, 'w', encoding='utf-8') as file:
file.write(content)
def convert_and_save(conversion_function, open_type, save_extension):
if content := open_file(open_type):
converted_content, line_count = conversion_function(content)
save_file(converted_content, save_extension)
messagebox.showinfo("转换完成", f"成功转换了 {line_count} 行数据。")
def create_gui():
root = tk.Tk()
root.title("数据转换工具")
frame = tk.Frame(root)
frame.pack(padx=80, pady=80)
dat_to_xyz_button = tk.Button(frame, text="DAT 转 XYZ", command=lambda: convert_and_save(convert_dat_to_xyz, "dat", "xyz"))
dat_to_xyz_button.grid(row=0, column=0, padx=5, pady=5)
xyz_to_dat_combined_button = tk.Button(frame, text="XYZ 转 DAT ", command=lambda: convert_and_save(convert_xyz_to_dat_combined, "xyz", "dat"))
xyz_to_dat_combined_button.grid(row=0, column=1, padx=5, pady=5)
author_label = tk.Label(frame, text="作者: 浅修 联系方式: 16699988885")
author_label.grid(row=2, column=0, columnspan=2, pady=10)
author_label = tk.Label(frame, text="区域公司: 第二区域公司")
author_label.grid(row=3, column=0, columnspan=2, pady=10)
root.mainloop()
if __name__ == "__main__":
create_gui()
2295

被折叠的 条评论
为什么被折叠?



