0

I'm trying to read a GTFS file and compare the value of a field with the same value for the next line. It should read line by line and when the trip_id of current line is the same of the last, append the value of the stop_id into a list. The code should jump to the next line when the stop_sequence is equal to 1. The result is an edge list to be analysed at a graph application (using graph theory).

Example of file's content:

"trip_id","arrival_time","departure_time","stop_id","stop_sequence"
"1156-10-0","07:00:00","07:00:00",940003729,1
"1156-10-0","07:01:30","07:01:30",940003730,2
"1156-10-0","07:03:00","07:03:00",940003731,3
"1156-10-1","07:04:30","07:04:30",940003767,1
"1156-10-1","07:06:00","07:06:00",940003886,2
"1156-10-1","07:07:30","07:07:30",940004427,3

the result should be:

940003729, 940003730
940003730, 940003731
-- jump to next trip_id --
940003767, 940003886
940003886, 940004427

My partial code:

def read_file():
    path = "file directory"
    data = open(path, "r")
    result = data.readline()
    search_comma = result.split(',')
    trip_id = search_comma[0]
    stop_id = search_comma[3]
    stop_sequence = search_comma[4]
    data.close()
    return trip_id, int(stop_id), int(stop_sequence)


old_trip, old_stop, old_sequence = read_file()


edge_list = []
for line in read_file():
    new_trip, new_stop, new_sequence = read_file()
    if old_trip == new_trip and new_sequence != 1:
        edge_list.append()
    next(read_file())

print(edge_list)

2 Answers 2

0

I would do this using the csv module to read the file, and itertools.groupby() to group the trips.

Something like this should do the trick:

import csv
import itertools
from operator import itemgetter

with open('/path/to/file') as f:
    reader = csv.DictReader(f)
    # group the rows by their trip_id
    for key, group in itertools.groupby(reader, key=itemgetter('trip_id')):
        print 'trip_id:', key
        stop_ids = [row['stop_id'] for row in group]
        # process the stop_ids in pairs
        for start, end in zip(stop_ids, stop_ids[1:]):
            print start, end

The output for the sample data is:

trip_id: 1156-10-0
940003729 940003730
940003730 940003731
trip_id: 1156-10-1
940003767 940003886
940003886 940004427

I believe you will be able to construct your edge list based on this example.

2
  • Thanks a lot for your help stranac. There's a way to make what I need using only python internal functions(no packages)? I want to keep my code as portable as possible future simulations. Feb 22, 2015 at 21:26
  • All of these are modules from the standard library (they come with every python installation)
    – stranac
    Feb 22, 2015 at 22:01
0

I solved the question the following way:

def gtfs_to_edge_list():
    rsource = "file.txt"
    wsource = "file2.txt"
    with open(rsource, "r") as data, open(wsource, "w") as target:
        # create two equal lists containing file' lines
        file1 = file2 = [line.strip() for line in data]

        # loop reading the two lists created, where the second list is read from the second line.
        for line1, line2 in zip(file1, file2[1:]):

            # select the first column from line 1 and 2 (position 0).
            trip_old = line1.split(',')[0]
            trip_new = line2.split(',')[0]

            # select the fourth column from line 1 and 2 (position 3).
            stop_old = line1.split(',')[3]
            stop_new = line2.split(',')[3]

            # Compare if trip_id of line 1 is equal to trip_id of line 2.
            if trip_old == trip_new:

                # if true, write stop_id from line 1 and 2 to target file. Trip_id 
                target.writelines([stop_old + ',', stop_new + '\n'])
                continue
        data.close()
        target.close()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.