writing a csv file

Discussion in 'Python' started by ejsaiet, Nov 12, 2012.

  1. ejsaiet

    ejsaiet Guest

    Hello,
    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)
    writer.writerows([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.
    Thanks
    E
     
    ejsaiet, Nov 12, 2012
    #1
    1. Advertisements

  2. Change the line print str(a) to print type(a), a
    You'll see what the problem is and be able to fix it.
     
    Mark Lawrence, Nov 12, 2012
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.