python 3.x - Need guide to coding CSV File -
now, have code parse xml website
def value2(): station in tree.findall('./stations/'): if station.tag == 'station': item1 in station: if item1.tag == 'stationid': print(item1.tag, item1.text) wf.writerow([item1.text]) ###need guide item2 in station: if item2.tag == 'nameth': print(item2.tag, item2.text) wf.writerow([item2.text]) ###need guide item3 in station: if item3.tag == 'nameen': print(item3.tag, item3.text) item4 in station: if item4.tag == 'areath': print(item4.tag, item4.text) item5 in station: if item5.tag == 'areaen': print(item5.tag, item5.text) item6 in station: if item6.tag == 'stationtype': print(item6.tag, item6.text) item7 in station: if item7.tag == 'lat': print(item7.tag, item7.text) wf.writerow([item1.text],[item2.text],[item3.text]) ###try test dates = time.strftime("%y%m%d") outpath = r"c:\users\gistda59\desktop\test" + "\\" + dates if not os.path.exists(outpath): os.makedirs(outpath) f = open(outpath + "\\" + "pm10daily-" + dates + ".csv","w",newline='') wf = csv.writer(f) wf.writerow (["stationid","nameth","nameen","areath","areaen","stationtype","latitude","longitude","date","time","pm10","o3","no2","so2","aqi"]) print ("\n\t\t***** xml csv on air4thai*****\n") air4thaiurl = 'http://air4thai.pcd.go.th/services/getaqi_xml.php?region=2' ### url ### resp = requests.get(air4thaiurl) msg = resp.content tree = et.fromstring(msg) value1() value2() f.close()
but need guide write on csv seem write on first column not write other column. try
wf.writerow([item1.text],[item2.text],[item3.text])
from code
it's not effective. need output csv thx , guide me.
Comments
Post a Comment