I have a number of rail networks, in the form of shapefiles of the "line" type. I want to turn them into a more abstract graph format, i.e. the kind with vertices and edges, not chart-type graphs. Moreover I want these data to be usable outside my GIS programs, in Python. Basically what I want is to extract something along the lines of the TSPLIB format, except with only vertices and edges. For my immediate purposes, I don't even need weights or anything. Just the topology.
Is there any relatively pain-free way to do this in QGIS or GRASS? I haven't quite lucked out yet.