title: How to implement Dijkstra's algorithm on dynamic data(input file) in place of static data in python?
body: <p>The code to find shortest path between two nodes of a graph:</p>
  
  <pre><code>    import heapq
      import sys
  
      class Graph:
  
  def __init__(self):
      self.vertices = {}
  
  def add_vertex(self, name, edges):
      self.vertices[name] = edges
  
  def shortest_path(self, start, finish):
      distances = {} # Distance from start to node
      previous = {}  # Previous node in optimal path from source
      nodes = [] # Priority queue of all nodes in Graph
  
      for vertex in self.vertices:
          if vertex == start: # Set root node as distance of 0
              distances[vertex] = 0
              heapq.heappush(nodes, [0, vertex])
          else:
              distances[vertex] = sys.maxint
              heapq.heappush(nodes, [sys.maxint, vertex])
          previous[vertex] = None
  
      while nodes:
          smallest = heapq.heappop(nodes)[1] # Vertex in nodes with smallest distance in distances
          if smallest == finish: # If the closest node is our target we're done so print the path
              path = []
              while previous[smallest]: # Traverse through nodes til we reach the root which is 0
                  path.append(smallest)
                  smallest = previous[smallest]
              return path
          if distances[smallest] == sys.maxint: # All remaining vertices are inaccessible from source
              break
  
          for neighbor in self.vertices[smallest]: # Look at all the nodes that this vertex is attached to
              alt = distances[smallest] + self.vertices[smallest][neighbor] # Alternative path distance
              if alt &lt; distances[neighbor]: # If there is a new shortest path update our priority queue (relax)
                  distances[neighbor] = alt
                  previous[neighbor] = smallest
                  for n in nodes:
                      if n[1] == neighbor:
                          n[0] = alt
                          break
                  heapq.heapify(nodes)
      return distances
  
  def __str__(self):
      return str(self.vertices)
  
  g = Graph()
  g.add_vertex('A', {'B': 7, 'C': 8})
  g.add_vertex('B', {'A': 7, 'F': 2})
  g.add_vertex('C', {'A': 8, 'F': 6, 'G': 4})
  g.add_vertex('D', {'F': 8})
  g.add_vertex('E', {'H': 1})
  g.add_vertex('F', {'B': 2, 'C': 6, 'D': 8, 'G': 9, 'H': 3})
  g.add_vertex('G', {'C': 4, 'F': 9}) 
  g.add_vertex('H', {'E': 1, 'F': 3})
  print g.shortest_path('A', 'F')
  &gt;&gt;&gt;['F','B']
  </code></pre>
  
  <p>Now I have an input file like :</p>
  
  <pre><code>London Birmingham 117
  Birmingham Bristol 85
  London Oxford 56
  San_Francisco San_Jose 50
  San_Jose Davis 270
  San_Francisco Davis 350
  END OF INPUT
  </code></pre>
  
  <p>So I tried using code like in place of the list in the end :</p>
  
  <pre><code>filename = open('Input1.txt', 'r')
  for line in filename:
  
  node1, node2, d = line.split()
  if node1!="END" and node2!="OF"
      print node1,node2,d
      g.add_vertex(node1,{node2:d})
      g.add_vertex(node2,{node1:d})
  </code></pre>
  
  <p>But I am getting integer error, maybe because I am not able to map the distance.So please tell me what changes I should do to get the code working for the input file and to print the distances along with the vertices.</p>