writing a csv file

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

  1. 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
     
    , Nov 12, 2012
    #1
    1. Advertising

  2. On 12/11/2012 00:05, wrote:
    >
    > 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
    >


    Change the line print str(a) to print type(a), a
    You'll see what the problem is and be able to fix it.

    --
    Cheers.

    Mark Lawrence.
     
    Mark Lawrence, Nov 12, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. HNguyen
    Replies:
    4
    Views:
    2,410
    HNguyen
    Dec 21, 2004
  2. Tintin92
    Replies:
    1
    Views:
    1,717
    Andrew Thompson
    Feb 14, 2007
  3. redcic
    Replies:
    6
    Views:
    345
    Dave Borne
    May 2, 2007
  4. jliu66
    Replies:
    0
    Views:
    517
    jliu66
    Oct 19, 2007
  5. Li Chen
    Replies:
    18
    Views:
    669
    Azmi Farih
    Mar 23, 2010
Loading...

Share This Page