Skip to content

Commit f369c96

Browse files
committed
Update to csv reader instead of fragile txt replace
1 parent 1c59929 commit f369c96

File tree

1 file changed

+21
-6
lines changed

1 file changed

+21
-6
lines changed

deployment/dataproc/gtfs-feed-fetcher/migrate_feeds_to_otpv1.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77
import logging
88
import zipfile
9+
import csv
910
import os
1011

1112
logging.basicConfig()
@@ -46,13 +47,27 @@ def modify_septa_bus():
4647
# Convert to buses to prevent error building OTP
4748
# Reference: https://github.com/septadev/GTFS?tab=readme-ov-file#routestxt
4849
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'
5053
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+
5671
LOG.debug('Modified %s', txt_path_to_update)
5772
except Exception as e:
5873
raise ValueError('Error modifying %s: %s', txt_path_to_update, e)

0 commit comments

Comments
 (0)