writing a csv file

E

ejsaiet

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
 
M

Mark Lawrence

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top