title: Loop with numbers in rows of the csv file
body: <p>I wrote a python script to run a sql in PostgreSQL,</p>
  
  <pre><code>import sys, os, math
  os.chdir(r'C:\Users\Heinz\Desktop')
  print os.getcwd()
  
  #set up psycopg2 environment
  import psycopg2
  
  #shortest_path module
  query = """
      select *
      from shortest_path ($$
          select
              gid as id,
              source::int4 as source,
              target::int4 as target,
              cost::double precision as cost,
              rcost::double precision as reverse_cost
          from network
          $$, %s, %s, %s, %s
      )
  """
  
  #make connection between python and postgresql
  conn = psycopg2.connect("dbname = 'test' user = 'postgres' host = 'localhost' password = 'xxxx'")
  cur = conn.cursor()
  
  #count rows in the table
  cur.execute("select count(*) from network")
  result = cur.fetchone()
  k = result[0] + 1                #number of points = number of segments + 1
  
  #run loops
  #import csv module
  import csv
  import tempfile
  
  element = []
  i = 1
  l = 1
  filename = 'pi_value.csv'
  with open(filename, 'wb') as f:
      while i &lt;= k:
          while l &lt;= k:
              cur.execute(query, (i, l, True, True))
              element = cur.fetchall()
              product = sum([a[-1] for a in element[:-1]])
              writer = csv.writer(f, delimiter = ',')
              writer.writerow([product])
              element = []
              l = l + 1
          l = 1
          i = i + 1
  </code></pre>
  
  <p>You can see that I used iterators from i to k(and l to k) to do the while loop, now I got a csv file containing numbers I want the <strong>iterator i and l</strong> to be. For example, here's the csv file,</p>
  
  <p><img src="https://i.stack.imgur.com/458SG.png" alt="enter image description here"></p>
  
  <p>I want the iterator to loop through using the number in every row starting from the first one, like in the innermost while loop, <strong>l = 6, l = 31, l = 28,..., l = 17, i is starting from 6 too, but only moves to i = 31 as l moves to 17 and back to l = 6, and so on.</strong></p>
  
  <p>How to write additional lines to read this csv file and let the iterator in the while loop to run loops as those numbers in the file？</p>
  
  <hr>
  
  <p><strong>Update#1</strong></p>
  
  <p>I tried this,</p>
  
  <pre><code>element = []
  
  with open('tc_sta_id.csv') as f1, open('pi_value.csv', 'wb') as f2:
  csvs = csv.reader(f1)
  col_num = 0
  rows = list(csvs)
  k = len(rows)
  for row in csvs:
      i = row[col_num]
      l = row[col_num]
      while i &lt;= k:
          while l &lt;= k:
              cur.execute(query, (i, l, True, True))
              element = cur.fetchall()
              product = sum([a[-1] for a in element[:-1]])
              writer = csv.writer(f2, delimiter = ',')
              writer.writerow([product])
              element = []
              l = l + 1
          l = row[col_num]
          i = i + 1
  </code></pre>
  
  <p>the script runs fine, but there are all blank in the output csv file, please give me suggestions to fix this! </p>