53 lines
2.2 KiB
Python
53 lines
2.2 KiB
Python
import csv
|
|
import argparse
|
|
from datetime import datetime
|
|
|
|
def process_territories(addresses_file, boundaries_file, final_file):
|
|
# Read the addresses and count occurrences of each TerritoryID
|
|
address_counts = {}
|
|
with open(addresses_file, 'r', encoding='utf-8-sig') as f:
|
|
reader = csv.DictReader(f)
|
|
for row in reader:
|
|
territory_id = row['TerritoryID']
|
|
if territory_id:
|
|
address_counts[territory_id] = address_counts.get(territory_id, 0) + 1
|
|
|
|
# Read the boundaries file and write to the final file
|
|
with open(boundaries_file, 'r', encoding='utf-8-sig') as f_in, \
|
|
open(final_file, 'w', newline='', encoding='utf-8') as f_out:
|
|
|
|
reader = csv.DictReader(f_in)
|
|
|
|
# Define the headers for the output file
|
|
fieldnames = ['TerritoryID', 'CategoryCode', 'Number', 'Area', 'Boundary', 'Address Count']
|
|
writer = csv.DictWriter(f_out, fieldnames=fieldnames)
|
|
writer.writeheader()
|
|
|
|
for row in reader:
|
|
territory_id = row['TerritoryID']
|
|
|
|
# Get the address count for the current territory
|
|
address_count = address_counts.get(territory_id, 0)
|
|
|
|
# Write the new row to the final file
|
|
writer.writerow({
|
|
'TerritoryID': territory_id,
|
|
'CategoryCode': row.get('CategoryCode', ''),
|
|
'Number': row.get('Number', ''),
|
|
'Area': row.get('Area', ''),
|
|
'Boundary': row.get('Boundary', ''),
|
|
'Address Count': address_count
|
|
})
|
|
|
|
if __name__ == '__main__':
|
|
parser = argparse.ArgumentParser(description='Process territory data.')
|
|
parser.add_argument('addresses_file', help='The path to the addresses CSV file.')
|
|
parser.add_argument('boundaries_file', help='The path to the boundaries CSV file.')
|
|
args = parser.parse_args()
|
|
|
|
# Generate the output file name
|
|
date_str = datetime.now().strftime('%b %Y')
|
|
output_file = f'Okinawa Territory {date_str} - Final.csv'
|
|
|
|
process_territories(args.addresses_file, args.boundaries_file, output_file)
|
|
print(f"Processing complete. Output written to {output_file}") |