title: Python beginner: read elements in one file and use them to modify another file
body: <p>I'm an economist with no programming background. I'm trying to learn how to use python because I've been told that it is very powerful for parsing data from websites. At the moment, I'm stuck with the following code and I would be extremely grateful for any suggestion.</p>
  
  <p>First of all, I wrote a code to parse the data from this table:</p>
  
  <p><a href="http://www.webifel.it/sifl/Tavola07.asp?comune=MILANO&amp;cod_istat=15146" rel="nofollow">http://www.webifel.it/sifl/Tavola07.asp?comune=MILANO&amp;cod_istat=15146</a></p>
  
  <p>The code I wrote is the following:</p>
  
  <pre><code>#!/usr/bin/env python
  
  from mechanize import Browser
  from BeautifulSoup import BeautifulSoup
  import urllib2, os
  
  def extract(soup):
  table = soup.find("table", cellspacing=2)
  for row in table.findAll('tr')[2:]:
          col = row.findAll('td')
          year = col[0].div.b.font.string
          detrazione = col[1].div.b.font.string
          ordinaria = col[2].div.b.font.string
          principale = col[3].div.b.font.string
          scopo = col[4].div.b.font.string
          record = (year, detrazione, ordinaria, principale, scopo)
          print &gt;&gt; outfile, "|".join(record)
  
  
  
  outfile = open("milano.txt", "w")
  br = Browser()
  br.set_handle_robots(False)
  url = "http://www.webifel.it/sifl/Tavola07.asp?comune=MILANO&amp;cod_istat=15146"
  page1 = br.open(url)
  html1 = page1.read()
  soup1 = BeautifulSoup(html1)
  extract(soup1)
  outfile.close()
  </code></pre>
  
  <p>The code reads the table, take only the information that I need and create a txt file. The code is pretty rudimental, but it accomplishes the job.</p>
  
  <p>My problem starts now. The url that I posted above is just one of the approximately 200 from which I need to parse the data.
  All the urls are differentiated by two elements only. Using the previous url:</p>
  
  <p><a href="http://www.webifel.it/sifl/Tavola07.asp?comune=MILANO&amp;cod_istat=15146" rel="nofollow">http://www.webifel.it/sifl/Tavola07.asp?comune=MILANO&amp;cod_istat=15146</a></p>
  
  <p>the two elements that uniquely identify this page are MILANO (the name of the city) and 15146 (a bureaucratic code).</p>
  
  <p>What I wanted to do was, first, creating a file with two columns:</p>
  
  <ol>
  <li>In the first the names of the cities I need;</li>
  <li>In the second the bureaucratic codes.</li>
  </ol>
  
  <p>Then, I wanted to create a loop in python that reads each line of this file, correctly modify the url in my code and perform the parsing task separately for each city.</p>
  
  <p>Do you have any suggestion about how to proceed? 
  Thanks in advance for any help and suggestion!</p>
  
  <p>[Update]</p>
  
  <p>Thanks to all for the helpful suggestions. I found the answer of Thomas K the most easy to implement for my knowledge of python. I still have problems, though.
  I modified the code in the following way:</p>
  
  <pre><code>#!/usr/bin/env python
  
  from mechanize import Browser
  from BeautifulSoup import BeautifulSoup
  import urllib2, os
  import csv
  
  def extract(soup):
  table = soup.find("table", cellspacing=2)
  for row in table.findAll('tr')[2:]:
          col = row.findAll('td')
          year = col[0].div.b.font.string
          detrazione = col[1].div.b.font.string
          ordinaria = col[2].div.b.font.string
          principale = col[3].div.b.font.string
          scopo = col[4].div.b.font.string
          record = (year, detrazione, ordinaria, principale, scopo)
          print &gt;&gt; outfile, "|".join(record)
  
  citylist = csv.reader(open("citycodes.csv", "rU"), dialect = csv.excel)
  for city in citylist:
  outfile = open("%s.txt", "w") % city
  br = Browser()
  br.set_handle_robots(False)
  url = "http://www.webifel.it/sifl/Tavola07.asp?comune=%s&amp;cod_istat=%s" % city
  page1 = br.open(url)
  html1 = page1.read()
  soup1 = BeautifulSoup(html1)
  extract(soup1)
  outfile.close()
  </code></pre>
  
  <p>where citycodes.csv is in the following format</p>
  
  <pre><code>MILANO;12345
  MODENA;67891
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
  File "modena2.py", line 25, in &lt;module&gt;
   outfile = open("%s.txt", "w") % city
  TypeError: unsupported operand type(s) for %: 'file' and 'list'
  </code></pre>
  
  <p>Thanks again!</p>