I want to access the values of a child in a WaterML file.
In this WaterML file, a child called values has several children as value. For example, the first element in the values has the variable value of 506.351. The problem is that I cannot access this value when using the below code in Python.
import requests
import xml.etree.ElementTree as ET
url = https://hs-apps.hydroshare.org/apps/nwm-forecasts/api/GetWaterML/?archive=rolling&config=analysis_assim&geom=forcing&variable=SWDOWN&COMID=2236,1144&lon=-98&lat=38.5&startDate=2018-07-23&endDate=2018-07-25&time=00&lag=t00z
r = requests.get(url)
root = ET.fromstring(r.content)
for child in root.iter('{http://www.cuahsi.org/waterML/1.1/}value'):
print(child.tag, child.attrib)
The output does not include the variable value (for example, 506.351)
I do appreciate any help or idea on how to access the variable values in this file.
You can use child.text to extract text surrounded by value tag.
import requests
import xml.etree.ElementTree as ET
url = 'https://hs-apps.hydroshare.org/apps/nwm-forecasts/api/GetWaterML/?archive=rolling&config=analysis_assim&geom=forcing&variable=SWDOWN&COMID=2236,1144&lon=-98&lat=38.5&startDate=2018-07-23&endDate=2018-07-25&time=00&lag=t00z'
requests.get(url)
root = ET.fromstring(r.content)
for child in root.iter('{http://www.cuahsi.org/waterML/1.1/}value'):
print(child.tag, child.attrib, child.text, '\n')
The output is:
{http://www.cuahsi.org/waterML/1.1/}value {'censorCode': 'nc', 'dateTime': '2018-07-23T00:00:00', 'timeOffset': '0', 'dateTimeUTC': '2018-07-23T00:00:00', 'methodCode': '1', 'sourceCode': '1', 'qualityControlLevelCode': '1'} 506.351
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments