Created
December 26, 2014 12:19
-
-
Save PirosB3/13716fdf34a449a2fd62 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import re | |
from collections import defaultdict | |
FIRST_NUMBER = re.compile('\s\d') | |
def main(file, type_name): | |
connections = defaultdict(list) | |
with open(file) as f: | |
for line in f.readlines(): | |
if len(line) == 0: | |
continue | |
delimiter = FIRST_NUMBER.search(line) | |
if not delimiter: | |
continue | |
delimiter = delimiter.start() | |
station_name = line[:delimiter] | |
times = line[delimiter+1:].strip().split(' ') | |
connections[station_name].extend(times) | |
print json.dumps({ | |
'type': type_name, | |
'data': connections | |
}) | |
if __name__ == '__main__': | |
import argparse | |
parser = argparse.ArgumentParser(description='Generate JSON from pdfs') | |
parser.add_argument('--type', type=str, required=True, help='Type of timetable') | |
parser.add_argument('--file', type=str, required=True, help='Path of timetable file') | |
args = parser.parse_args() | |
main(args.file, args.type) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment