1
http://www.srh.noaa.gov/data/obhistory/PAFA.html의 NOAA 데이터로 CSV 파일을 만들려고합니다.NOAA 데이터 및 CSV 파일 작성
현재로서는 CSV 파일을 쓰는 데 문제가 있습니다.
import urllib2 as urllib
from bs4 import BeautifulSoup
from time import localtime, strftime
import csv
url = 'http://www.srh.noaa.gov/data/obhistory/PAFA.html'
file_pointer = urllib.urlopen(url)
soup = BeautifulSoup(file_pointer)
table = soup('table')[3]
table_rows = table.findAll('tr')
row_count = 0
for table_row in table_rows:
row_count += 1
if row_count < 4:
continue
date = table_row('td')[0].contents[0]
time = table_row('td')[1].contents[0]
wind = table_row('td')[2].contents[0]
print date, time, wind
with open("/home/eyalak/Documents/weather/weather.csv", "wb") as f:
writer = csv.writer(f)
print date, time, wind
writer.writerow(('Title 1', 'Title 2', 'Title 3'))
writer.writerow(str(time)+str(wind)+str(date)+'\n')
if row_count == 74:
print "74"
인쇄 된 결과가 양호합니다. 그렇지 않은 파일입니다. 내가 얻을 :
Title 1,Title 2,Title 3
0,5,:,5,3,C,a,l,m,0,8,"
만든 CSV 파일에 문제가 있습니다 : 1. 제목이 잘못된 컬럼으로 구분되며, 데이터는 쉼표 2. 열 2, "제목이"대 "1, 제목을"이 잘못된 곳에서 윤곽을 잡습니다. 3. 스크립트는 맨 아래에 추가하는 대신 이전 행에 작성하는 새 줄을 작성합니다. 이견있는 사람?