2016年4月20日 星期三

Python & Google Fusion Tables to show Taiwan PM2.5 on Google Map

將讀取到的檔案存成.cvs後
上傳Google Fusion Tables (Reference:here)


python 原始碼:

import csv 
import urllib
import json
new_data = [['SiteName','address','PM2.5']]



with open('AQXSite.csv', 'r') as fin:
    data = fin.read().decode('utf-8').split('\n')
for line in data[1:]:
    site=[]
    rows = line.split(',')
    site.append(rows[0].encode('utf-8'))
    site.append(rows[5].encode('utf-8'))
    new_data.append(site)




url = 'http://opendata.epa.gov.tw/ws/Data/AQX/?format=json'
urllib.urlretrieve(url, "data.json")
data_str = open('data.json', 'r').read().decode('utf-8')
AQ_data = json.loads(data_str)

for pm in AQ_data:
    for data in new_data:
        if pm["SiteName"]==data[0].decode('utf-8'):
            data.append(str(pm["PM2.5"]))
            break

for k in range(len(new_data)):
    print new_data[k][0],"\t\t",new_data[k][1],"\t\t",new_data[k][2]


#write

f = open("data.csv","wb")
w = csv.writer(f)
w.writerows(new_data)  
f.close() 

沒有留言:

張貼留言