Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #33151 > unrolled thread

writing a csv file

Started byejsaiet@alaska.edu
First post2012-11-11 16:05 -0800
Last post2012-11-12 00:27 +0000
Articles 2 — 2 participants

Back to article view | Back to comp.lang.python


Contents

  writing a csv file ejsaiet@alaska.edu - 2012-11-11 16:05 -0800
    Re: writing a csv file Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-11-12 00:27 +0000

#33151 — writing a csv file

Fromejsaiet@alaska.edu
Date2012-11-11 16:05 -0800
Subjectwriting a csv file
Message-ID<4467d3df-7d0c-4566-8fed-ba7fb8b54afe@googlegroups.com>
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

[toc] | [next] | [standalone]


#33153

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2012-11-12 00:27 +0000
Message-ID<mailman.3567.1352679962.27098.python-list@python.org>
In reply to#33151
On 12/11/2012 00:05, ejsaiet@alaska.edu 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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web