ChatGPT: write a Python code to search all YYYYMM_ALL.TXT
May 18, 2023 12:18:25 GMT
Richard G7OED likes this
Post by Wolfgang OE1MWW on May 18, 2023 12:18:25 GMT
JTDX is writing a text file, named YYYYMM_ALL.TXT (one for each YYYY year and MM month)
searching all those files for a call sign to find out if you worked before (B4) was my intend.
I have only a vague idea how to program in Python, but I asked ChatGPT to write me a
Python source code. After the first version I asked for changes and extensions and finally,
step by step, it became a very usable program.
a.) copy the text between the '=' lines into a text file
b.) modify the path (that's the one where the JTDX.ini is) and change the callsign
a few lines further down.
c.) save text file as 'callsearch.py'
d.) open a 'cmd' window and call it with 'python callsearch.py'
Additional to the result in the GUI, a text file with the results is written
Iy you are happy with your python file, you may compile it with
'pyinstaller --onefile callsearch.py'. The resulting .exe is to be found in the subdirectory \dist
73's de OE1MWW
Wolfgang
====================================================================
import tkinter as tk
from tkinter import filedialog
import glob
from datetime import datetime
import os
SEARCH_PATH = r'c:\Users\wkm\AppData\Local\JTDX\\'
def search_files(pattern, search_string):
file_list = glob.glob(pattern)
result_text.delete("1.0", tk.END) # Clear previous results
results = []
for file_path in file_list:
with open(file_path, 'r') as file:
for line in file:
if search_string in line and 'OE1MWW' in line:
result = f"{os.path.basename(file_path)}: {line.strip()}"
result_text.insert(tk.END, result + "\n") # Add line break
results.append(result)
save_results(results, search_string)
def save_results(results, search_string):
current_datetime = datetime.now()
file_name = f"callsearch-{search_string}-{current_datetime.strftime('%Y-%m-%d_%H-%M-%S')}.txt"
with open(file_name, "w") as file:
file.write("\n".join(results) + "\n") # Write each result as a separate line with an additional line break
def search_button():
pattern = SEARCH_PATH + '*.txt'
search_string = search_entry.get()
search_files(pattern, search_string)
def clear_button():
result_text.delete("1.0", tk.END) # Clear the result window
# Create the GUI window
window = tk.Tk()
window.title("Text File Search")
# Create the explanation label
explanation_label = tk.Label(window, text="Enter a call sign and click 'Search' to find matching lines in the files.")
explanation_label.grid(row=0, column=0, columnspan=4)
# Create the search string label and entry
search_label = tk.Label(window, text="Search String:")
search_label.grid(row=1, column=0)
search_entry = tk.Entry(window)
search_entry.grid(row=1, column=1)
# Create the search button
search_button = tk.Button(window, text="Search", command=search_button)
search_button.grid(row=1, column=2)
# Create the clear button
clear_button = tk.Button(window, text="Clear", command=clear_button)
clear_button.grid(row=1, column=3)
# Create the search result text box with horizontal scroll
result_label = tk.Label(window, text="Search Results:")
result_label.grid(row=2, column=0, columnspan=4)
result_text = tk.Text(window, height=20, width=100, wrap=tk.NONE)
result_text.grid(row=3, column=0, columnspan=4)
# Add horizontal scroll to the result text box
scrollbar = tk.Scrollbar(window, orient=tk.HORIZONTAL, command=result_text.xview)
scrollbar.grid(row=4, column=0, columnspan=4, sticky="ew")
result_text.configure(xscrollcommand=scrollbar.set)
# Run the GUI event loop
window.mainloop()
from tkinter import filedialog
import glob
from datetime import datetime
import os
SEARCH_PATH = r'c:\Users\wkm\AppData\Local\JTDX\\'
def search_files(pattern, search_string):
file_list = glob.glob(pattern)
result_text.delete("1.0", tk.END) # Clear previous results
results = []
for file_path in file_list:
with open(file_path, 'r') as file:
for line in file:
if search_string in line and 'OE1MWW' in line:
result = f"{os.path.basename(file_path)}: {line.strip()}"
result_text.insert(tk.END, result + "\n") # Add line break
results.append(result)
save_results(results, search_string)
def save_results(results, search_string):
current_datetime = datetime.now()
file_name = f"callsearch-{search_string}-{current_datetime.strftime('%Y-%m-%d_%H-%M-%S')}.txt"
with open(file_name, "w") as file:
file.write("\n".join(results) + "\n") # Write each result as a separate line with an additional line break
def search_button():
pattern = SEARCH_PATH + '*.txt'
search_string = search_entry.get()
search_files(pattern, search_string)
def clear_button():
result_text.delete("1.0", tk.END) # Clear the result window
# Create the GUI window
window = tk.Tk()
window.title("Text File Search")
# Create the explanation label
explanation_label = tk.Label(window, text="Enter a call sign and click 'Search' to find matching lines in the files.")
explanation_label.grid(row=0, column=0, columnspan=4)
# Create the search string label and entry
search_label = tk.Label(window, text="Search String:")
search_label.grid(row=1, column=0)
search_entry = tk.Entry(window)
search_entry.grid(row=1, column=1)
# Create the search button
search_button = tk.Button(window, text="Search", command=search_button)
search_button.grid(row=1, column=2)
# Create the clear button
clear_button = tk.Button(window, text="Clear", command=clear_button)
clear_button.grid(row=1, column=3)
# Create the search result text box with horizontal scroll
result_label = tk.Label(window, text="Search Results:")
result_label.grid(row=2, column=0, columnspan=4)
result_text = tk.Text(window, height=20, width=100, wrap=tk.NONE)
result_text.grid(row=3, column=0, columnspan=4)
# Add horizontal scroll to the result text box
scrollbar = tk.Scrollbar(window, orient=tk.HORIZONTAL, command=result_text.xview)
scrollbar.grid(row=4, column=0, columnspan=4, sticky="ew")
result_text.configure(xscrollcommand=scrollbar.set)
# Run the GUI event loop
window.mainloop()
====================================================================