writing a csv file

ejsaiet at alaska.edu ejsaiet at alaska.edu
Mon Nov 12 01:05:52 CET 2012

I have the script below, which it extracts NOAA data from HTML and is planed writes it to a CSV file. Here is the script:

import urllib2
from bs4 import BeautifulSoup
from time import localtime, strftime
import csv

#This script is intended to retrive NOAA data and apend it to a csv file.

# Wait 45 min
#Need to work on this part...

# Go into URL
page = urllib2.urlopen("http://w1.weather.gov/obhistory/PAFA.html")
soup = BeautifulSoup(page)
datemonth=strftime("%m", localtime())
dateday=strftime("%d", localtime())

with open("/home/eyalak/Documents/weather/weather.csv", "wb") as f:
      writer = csv.writer(f)
      table = soup.findAll("table")[3]
      #print table
      for tr in table.findAll("tr", valign="top"):
         a={x.string for x in tr.findAll('td')}
         print str(a)

It did not work unless I changed the line a={x.string for x in tr.findAll('td')} to a=list({x.string for x in tr.findAll('td')})

But that disorganizes the data. How can I write the data to a csv file without altering the order prior to the list function.

More information about the Python-list mailing list