Mar-19-2024, 12:30 PM
I've got a code for formatting and parsing data (the code doesn't look nice but I started learning python last week, all I want just now is functionality), but I've came across a problem that any numbers that are in the text file are written to the excel file as text, which means I need to manually change it (I made a python code so I didn't need to do anything in excel). Could someone help? Here is my code:
import os import tkinter as tk from tkinter import ttk from tkinter import messagebox import csv import openpyxl class data_formatter(): def __init__(self): read_path=os.listdir('E:/DATALOGGING/') self.root=tk.Tk() self.root.geometry('350x150') self.root.title('Data Formatter') self.root.iconbitmap('Y:/ENGINEERING/DATALOGGING/Files/datalog.ico') self.mainframe=tk.Frame(self.root,background='white') self.mainframe.pack(fill='both',expand=True) self.type_label=ttk.Label(self.mainframe,text='Select data type: ',background='white',pad=10) self.type_label.grid(row=0, column=0, padx=70) self.type_box=ttk.Combobox(self.mainframe, values=read_path) self.type_box.grid(row=1,column=0) driveButton=ttk.Button(self.mainframe,text='Format',command=self.format_data) driveButton.grid(row=2,column=2) self.graph_check=tk.Checkbutton(self.mainframe, text='Add a graph', background='white') self.graph_check.grid(row=2,column=0, pady=10) self.root.mainloop() return def format_data(self): data_type=self.type_box.get() read_path=f'E:/DATALOGGING/{data_type}' new_file=(f'Y:/ENGINEERING/DATALOGGING/{data_type}') if os.path.exists(new_file)==False: os.mkdir(new_file) else: os.chdir(new_file) write_text_path=f'{new_file}/Text' write_excel_path=f'{new_file}/Excel' file_list=os.listdir(f'{read_path}/') for file in file_list: write_path=f'{write_text_path}/Parsed_{file}' if os.path.exists(write_text_path): os.chdir(write_text_path) os.chdir(write_excel_path) else: os.mkdir(write_text_path) os.mkdir(write_excel_path) with open(f'{read_path}/{file}', 'r') as read_file, open(write_path, 'w') as write_file: read_file=read_file.readlines() error_count=0 for line in read_file: line=line.strip() if line.find('ERROR')!=-1: error_count+=1 else: write_file.write(f'{line}\n') write_file.write(f'\nErrors: {error_count}\n') write_file.close() excel_file=file.replace('.txt', '.xlsx') xlsx_file=f'{new_file}/Excel/Parsed_{excel_file}' wb=openpyxl.Workbook() ws=wb.worksheets[0] with open(write_path, 'r') as data: reader=csv.reader(data, delimiter=' ') for row in reader: ws.append(row) wb.save(xlsx_file) if write_text_path and write_excel_path: messagebox.showinfo('Datalog Formatter', 'Formatting successful!') else: messagebox.showerror('Datalog Formatter', 'Formatting failed! Check all used files and directories.') if __name__ == '__main__': data_formatter()