|
6 | 6 | """ |
7 | 7 | import logging |
8 | 8 | import zipfile |
| 9 | +import csv |
9 | 10 | import os |
10 | 11 |
|
11 | 12 | logging.basicConfig() |
@@ -46,13 +47,27 @@ def modify_septa_bus(): |
46 | 47 | # Convert to buses to prevent error building OTP |
47 | 48 | # Reference: https://github.com/septadev/GTFS?tab=readme-ov-file#routestxt |
48 | 49 | txt_path_to_update = os.path.join(gtfs_dir, 'routes.txt') |
49 | | - content='' |
| 50 | + column_to_update = 'route_type' |
| 51 | + new_route_type = '11' |
| 52 | + otpv1_supported_route_type = '3' |
50 | 53 | try: |
51 | | - with open(txt_path_to_update, 'r') as file: |
52 | | - content = file.read() |
53 | | - content = content.replace(',,11,,', ',,3,,') |
54 | | - with open(txt_path_to_update, 'w') as file: |
55 | | - file.write(content) |
| 54 | + updated_rows = [] |
| 55 | + with open(txt_path_to_update, 'r', newline='', encoding='utf-8') as csvfile: |
| 56 | + reader = csv.DictReader(csvfile) |
| 57 | + header = reader.fieldnames |
| 58 | + if column_to_update not in header: |
| 59 | + raise ValueError(f"Column '{column_to_update}' not found in routes.txt") |
| 60 | + for row in reader: |
| 61 | + if row[column_to_update] == str(new_route_type): |
| 62 | + row[column_to_update] = str(otpv1_supported_route_type) |
| 63 | + LOG.info(f"Converted route_id '{row['route_id']}' from type {new_route_type} to {otpv1_supported_route_type}") |
| 64 | + updated_rows.append(row) |
| 65 | + |
| 66 | + with open(txt_path_to_update, 'w', newline='', encoding='utf-8') as csvfile: |
| 67 | + writer = csv.DictWriter(csvfile, fieldnames=header) |
| 68 | + writer.writeheader() |
| 69 | + writer.writerows(updated_rows) |
| 70 | + |
56 | 71 | LOG.debug('Modified %s', txt_path_to_update) |
57 | 72 | except Exception as e: |
58 | 73 | raise ValueError('Error modifying %s: %s', txt_path_to_update, e) |
|
0 commit comments