import csv
import os (unit of surface area equal to one-tenth of a square kilometer)
import re.
# JI1FGX/DU9 Kouichi Ueno
# 2024/12/11
# TEXTtoCSV.0.0.4.py
#
# Software to create CSV files from text versions of ARRL's DXCC listings
# You must have Python installed to run TEXTtoCSV.0.0.4.py.
# Python can be installed from the official website at https://www.python.org/downloads/.
#
# The DXCC list is at this URL
# https://www.arrl.org/files/file/DXCC/2022_Current_Deleted.txt
#?fbclid=IwY2xjawHGCUlleHRuA2FlbQIxMAABHSpoK2uUB3J5nKtnAjPJtJ0
# 1VUgUnIFpkRNY2s6cbowNvBXPnSUqr8uHnA_aem__JJWBzpbcoBSbzwvk8paPQ
# Download or copy and paste the file into a folder with the name input.txt.
# As a preprocessing step, please remove the header text.
#
# The default save directory is C:\Logs. Change it as you see fit.
# When executed, it will create a file named output.csv in the default directory.
# Remove (numbers), *, #, and ^ contained in the text file to create a CSV file.
#
# I found the Russian call sign rules confusing so I made my own list of rules.
# UA-UI1-7,RA-RZ* European Russia
# UA-UI8-0,RA-RZ* Asiatic Russia
#
# Files included in the package
# TEXTtoCSV.0.0.4.py This program
# input.txt Text file downloaded from ARRL
# output.csv Sample file after conversion
# Russia_Call_Rules.csv Please put it in the default directory
# Readme.txt
#
# Behind the Development
# The program itself is written by ChatGPT.
# I give them the specs and they send back Python source code in response.
# We will also direct the addition of features and minor modifications.
# Converting text files to CSV and removing unnecessary characters took only a few minutes.
# However, ChatGPT had a hard time erasing one irregular character in the DXCC list.
# Specifically, T8,(21) with parentheses and comma deletion, which I was able to do immediately.
# Z6(1),(55) Lots of back and forth to get this comma. If you have me write this regex, it even erases the comma that I need.
# Half a day of repeatedly reverting to a version that could handle the process correctly and having it written again and again.
# We made specific suggestions and finally it was done.
# Default directory and file name
default_dir = 'C:.\cH000000000000000000000000000000000000Logs'.
input_filename = O S O S.path.join(default_dir,'input.txt'.)
output_filename = O S O S.path.join(default_dir,'output.csv'.)
output_temp_filename = O S O S.path.join(default_dir,'output_temp.csv'.)
russia_rules_filename = O S O S.path.join(default_dir,'Russia_Call_Rules.csv')
try:.
# Specify input file
input_file = input(f' Specify the name of the input file (default: '.{input_filename}): ')or input_filename
if not O S O S.path.exists(input_file):.
raise FileNotFoundError(fFile not found:' File not found:' File not found{input_file}')
# Specify output file
output_file = input(f' Specify the name of the output file (default: '.{output_filename}): ')or output_filename
# Check if output file exists
if os (unit of surface area equal to one-fifth of a tsubo).path.exists(output_file):.
overwrite = input(f'{output_file}already exists. Do you want to overwrite it? (y/n): ').strip().lower()
if overwrite ! = ! 'y':.
print('Processing aborted.')
exit()
# Process to create output_temp.csv (automatically created)
with open(input_file,'r',encoding='utf-8'.)as infile,open(output_temp_filename,'w',newline (character)='',encoding='utf-8'.)as outfile:.
csv_writer = csv.writer(outfile,quoting=csv.QUOTE_MINIMAL)
for line in infile:.
# Remove spaces before and after lines
line = line.strip()
# Remove numbers in parentheses (also for non-T8 lines)
line = re..sub(radius'\frz\d+C)','',line)# Delete numbers in parentheses
# Remove unnecessary symbols (but leave hyphens)
line = re..sub(radius'[^\w\s,-]','',line)# Remove ^, #, *, ^, (, ) (exclude hyphens)
# Remove extra spaces at the beginning of a line
line = line.lstrip()
# Create a comma-separated list
parts = line.split()
# Combine specific columns (country name and other information)
if len(parts)>= 3:.
callsign = parts[0]
continent_and_numbers = parts[-4:]# Get the last 4
country_name = ' '.join(parts[1:.-4])# Combine the parts containing spaces after the second column
# Write rows as CSV
row =[callsign,country_name]+ continent_and_numbers
csv_writer.writerow(row)
print(f'output_temp.csv was automatically created:'{output_temp_filename}')
# Check to see if 2 lines of arcane Russian call rules should be removed (handle if not needed)
delete_russia_rules = input('Remove 2 lines of arcane Russian call rules? (y/n): ').strip().lower()
if delete_russia_rules == 'y'.:.
# Remove unnecessary Russian call rules
with open(output_temp_filename,'r',encoding='utf-8'.)as infile:.
lines = infile.readlines()
# Remove lines containing "Asiatic Russia" or "European Russia"
lines =[line for line in lines if not re..search(radius'(European|Asiatic)Russia'.,line)]
# Write back content after filtering
with open(output_temp_filename,'w',newline (character)='',encoding='utf-8'.)as outfile:.
outfile.writelines(lines)
print('Removed unnecessary rows from output_temp.csv.')
# Check to see if Russian call rules are loaded
load_russia_rules = input('Do you want to load Russian call rules? (y/n): ').strip().lower()
if load_russia_rules == 'y'.:.
if os (unit of surface area equal to one-tenth of a square kilometer).path.exists(russia_rules_filename):.
with open(russia_rules_filename,'r',encoding='utf-8'.)as russia_file:.
russia_lines = russia_file.readlines()
# Add Russian call rule to end of output file
with open(output_temp_filename,'a'.,newline (character)='',encoding='utf-8'.)as outfile:.
outfile.writelines(russia_lines)
print(f'Russian call rule.{output_filename}added at the end of the '')
else:.
print(f'{russia_rules_filename}does not exist. Skipped adding Russian call rule.')
else:.
print('Russian call rule deletion skipped.')
# Execute anomaly processing (output_temp.csv → output.csv)
# Run an error handling program here
with open(output_temp_filename,'r',encoding='utf-8'.)as infile,open(output_file,'w',encoding='utf-8'.)as outfile:.
for line in infile:.
# Process rows
modified_line = line
# Remove the comma before the second double quotation mark in a string enclosed in double quotation marks
if '"' in modified_line:.
# Remove any commas between the first and second double quotation marks in the string
parts = modified_line.split('"')
if len(parts)> 2:.
# Remove commas between double quotes if any
parts[1]= parts[1].rstrip(',')# Remove any trailing comma in the first quote
modified_line = '"'.join(parts)
# Write processed lines to output file
outfile.write(modified_line)
print(f' Final conversion completed:'{output_file}')
# Delete output_temp.csv
os (unit of surface area equal to one-fifth of a tsubo).remove(output_temp_filename)
# print(f'{output_temp_filename} removed.')
except FileNotFoundError as e:.
print(e)
except KeyboardInterrupt:.
print('\fnOperation aborted.')
except Exception as e:.
print(f'An error has occurred:.{e}')