Loop through a complicated xml data to get specific values using python
I want to get the value temperature
which is in the <element type = 'air_temperature_maximum'>
under <forecast-period>
. I only want it for the area 'Melbourne'
.
xml source = http://www.bom.gov.au/fwo/IDV10753.xml
I tried the following, but this only prints out the entire parsed xml rather than what i intend to get.
import xml.etree.ElementTree as ET
import requests
url = "http://www.bom.gov.au/fwo/IDV10753.xml"
response = requests.get(url, verify=False).content.decode('UTF-8')
tree = ET.parse(response)
print(tree.find('product').find('amoc').find('forecast').find('area')
.find('forecast-period').find('element').text)
I want all the 7 day temperature value which is in <element type = 'air_temperature_maximum'>
for the area 'Melbourne'
. Any help is much appreciated.
python xml
add a comment |
I want to get the value temperature
which is in the <element type = 'air_temperature_maximum'>
under <forecast-period>
. I only want it for the area 'Melbourne'
.
xml source = http://www.bom.gov.au/fwo/IDV10753.xml
I tried the following, but this only prints out the entire parsed xml rather than what i intend to get.
import xml.etree.ElementTree as ET
import requests
url = "http://www.bom.gov.au/fwo/IDV10753.xml"
response = requests.get(url, verify=False).content.decode('UTF-8')
tree = ET.parse(response)
print(tree.find('product').find('amoc').find('forecast').find('area')
.find('forecast-period').find('element').text)
I want all the 7 day temperature value which is in <element type = 'air_temperature_maximum'>
for the area 'Melbourne'
. Any help is much appreciated.
python xml
add a comment |
I want to get the value temperature
which is in the <element type = 'air_temperature_maximum'>
under <forecast-period>
. I only want it for the area 'Melbourne'
.
xml source = http://www.bom.gov.au/fwo/IDV10753.xml
I tried the following, but this only prints out the entire parsed xml rather than what i intend to get.
import xml.etree.ElementTree as ET
import requests
url = "http://www.bom.gov.au/fwo/IDV10753.xml"
response = requests.get(url, verify=False).content.decode('UTF-8')
tree = ET.parse(response)
print(tree.find('product').find('amoc').find('forecast').find('area')
.find('forecast-period').find('element').text)
I want all the 7 day temperature value which is in <element type = 'air_temperature_maximum'>
for the area 'Melbourne'
. Any help is much appreciated.
python xml
I want to get the value temperature
which is in the <element type = 'air_temperature_maximum'>
under <forecast-period>
. I only want it for the area 'Melbourne'
.
xml source = http://www.bom.gov.au/fwo/IDV10753.xml
I tried the following, but this only prints out the entire parsed xml rather than what i intend to get.
import xml.etree.ElementTree as ET
import requests
url = "http://www.bom.gov.au/fwo/IDV10753.xml"
response = requests.get(url, verify=False).content.decode('UTF-8')
tree = ET.parse(response)
print(tree.find('product').find('amoc').find('forecast').find('area')
.find('forecast-period').find('element').text)
I want all the 7 day temperature value which is in <element type = 'air_temperature_maximum'>
for the area 'Melbourne'
. Any help is much appreciated.
python xml
python xml
edited Jan 2 at 4:23
Bharath
asked Jan 2 at 4:12
BharathBharath
717
717
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
You can iterate through the XML by brute force in multiple nested loops:
from xml.etree.ElementTree import fromstring, ElementTree
from requests import get
url = 'http://www.bom.gov.au/fwo/IDV10753.xml'
req = get(url)
tree = ElementTree(fromstring(req.text))
root = tree.getroot()
for outer in root:
if outer.tag == 'forecast':
for inner in outer:
if inner.attrib['description'] == 'Melbourne':
for element in inner:
for temp in element:
if temp.attrib["type"] == 'air_temperature_maximum':
print(temp.text)
Which gives 7 temperatures:
23
28
42
24
22
24
27
You can also store the temperatures in a list using a list comprehension:
for outer in root:
if outer.tag == "forecast":
for inner in outer:
if inner.attrib["description"] == "Melbourne":
temps = [
temp.text
for element in inner
for temp in element
if temp.attrib["type"] == "air_temperature_maximum"
]
print(temps)
List of temperatures:
['23', '28', '42', '24', '22', '24', '27']
I'll leave the end conversion of these temperatures to you.
1
it's like you read my mind. I was thinking how to convert that into a list. You're a legend mate! Thank you.
– Bharath
Jan 2 at 23:53
1
@Bharath No worries man. Your question was clear and understandable, and I immediately thought showing a list example would be beneficial to you. When good questions are asked, good answers always come :).
– RoadRunner
Jan 3 at 6:24
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001108%2floop-through-a-complicated-xml-data-to-get-specific-values-using-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can iterate through the XML by brute force in multiple nested loops:
from xml.etree.ElementTree import fromstring, ElementTree
from requests import get
url = 'http://www.bom.gov.au/fwo/IDV10753.xml'
req = get(url)
tree = ElementTree(fromstring(req.text))
root = tree.getroot()
for outer in root:
if outer.tag == 'forecast':
for inner in outer:
if inner.attrib['description'] == 'Melbourne':
for element in inner:
for temp in element:
if temp.attrib["type"] == 'air_temperature_maximum':
print(temp.text)
Which gives 7 temperatures:
23
28
42
24
22
24
27
You can also store the temperatures in a list using a list comprehension:
for outer in root:
if outer.tag == "forecast":
for inner in outer:
if inner.attrib["description"] == "Melbourne":
temps = [
temp.text
for element in inner
for temp in element
if temp.attrib["type"] == "air_temperature_maximum"
]
print(temps)
List of temperatures:
['23', '28', '42', '24', '22', '24', '27']
I'll leave the end conversion of these temperatures to you.
1
it's like you read my mind. I was thinking how to convert that into a list. You're a legend mate! Thank you.
– Bharath
Jan 2 at 23:53
1
@Bharath No worries man. Your question was clear and understandable, and I immediately thought showing a list example would be beneficial to you. When good questions are asked, good answers always come :).
– RoadRunner
Jan 3 at 6:24
add a comment |
You can iterate through the XML by brute force in multiple nested loops:
from xml.etree.ElementTree import fromstring, ElementTree
from requests import get
url = 'http://www.bom.gov.au/fwo/IDV10753.xml'
req = get(url)
tree = ElementTree(fromstring(req.text))
root = tree.getroot()
for outer in root:
if outer.tag == 'forecast':
for inner in outer:
if inner.attrib['description'] == 'Melbourne':
for element in inner:
for temp in element:
if temp.attrib["type"] == 'air_temperature_maximum':
print(temp.text)
Which gives 7 temperatures:
23
28
42
24
22
24
27
You can also store the temperatures in a list using a list comprehension:
for outer in root:
if outer.tag == "forecast":
for inner in outer:
if inner.attrib["description"] == "Melbourne":
temps = [
temp.text
for element in inner
for temp in element
if temp.attrib["type"] == "air_temperature_maximum"
]
print(temps)
List of temperatures:
['23', '28', '42', '24', '22', '24', '27']
I'll leave the end conversion of these temperatures to you.
1
it's like you read my mind. I was thinking how to convert that into a list. You're a legend mate! Thank you.
– Bharath
Jan 2 at 23:53
1
@Bharath No worries man. Your question was clear and understandable, and I immediately thought showing a list example would be beneficial to you. When good questions are asked, good answers always come :).
– RoadRunner
Jan 3 at 6:24
add a comment |
You can iterate through the XML by brute force in multiple nested loops:
from xml.etree.ElementTree import fromstring, ElementTree
from requests import get
url = 'http://www.bom.gov.au/fwo/IDV10753.xml'
req = get(url)
tree = ElementTree(fromstring(req.text))
root = tree.getroot()
for outer in root:
if outer.tag == 'forecast':
for inner in outer:
if inner.attrib['description'] == 'Melbourne':
for element in inner:
for temp in element:
if temp.attrib["type"] == 'air_temperature_maximum':
print(temp.text)
Which gives 7 temperatures:
23
28
42
24
22
24
27
You can also store the temperatures in a list using a list comprehension:
for outer in root:
if outer.tag == "forecast":
for inner in outer:
if inner.attrib["description"] == "Melbourne":
temps = [
temp.text
for element in inner
for temp in element
if temp.attrib["type"] == "air_temperature_maximum"
]
print(temps)
List of temperatures:
['23', '28', '42', '24', '22', '24', '27']
I'll leave the end conversion of these temperatures to you.
You can iterate through the XML by brute force in multiple nested loops:
from xml.etree.ElementTree import fromstring, ElementTree
from requests import get
url = 'http://www.bom.gov.au/fwo/IDV10753.xml'
req = get(url)
tree = ElementTree(fromstring(req.text))
root = tree.getroot()
for outer in root:
if outer.tag == 'forecast':
for inner in outer:
if inner.attrib['description'] == 'Melbourne':
for element in inner:
for temp in element:
if temp.attrib["type"] == 'air_temperature_maximum':
print(temp.text)
Which gives 7 temperatures:
23
28
42
24
22
24
27
You can also store the temperatures in a list using a list comprehension:
for outer in root:
if outer.tag == "forecast":
for inner in outer:
if inner.attrib["description"] == "Melbourne":
temps = [
temp.text
for element in inner
for temp in element
if temp.attrib["type"] == "air_temperature_maximum"
]
print(temps)
List of temperatures:
['23', '28', '42', '24', '22', '24', '27']
I'll leave the end conversion of these temperatures to you.
edited Jan 2 at 4:47
answered Jan 2 at 4:33
RoadRunnerRoadRunner
11.3k31341
11.3k31341
1
it's like you read my mind. I was thinking how to convert that into a list. You're a legend mate! Thank you.
– Bharath
Jan 2 at 23:53
1
@Bharath No worries man. Your question was clear and understandable, and I immediately thought showing a list example would be beneficial to you. When good questions are asked, good answers always come :).
– RoadRunner
Jan 3 at 6:24
add a comment |
1
it's like you read my mind. I was thinking how to convert that into a list. You're a legend mate! Thank you.
– Bharath
Jan 2 at 23:53
1
@Bharath No worries man. Your question was clear and understandable, and I immediately thought showing a list example would be beneficial to you. When good questions are asked, good answers always come :).
– RoadRunner
Jan 3 at 6:24
1
1
it's like you read my mind. I was thinking how to convert that into a list. You're a legend mate! Thank you.
– Bharath
Jan 2 at 23:53
it's like you read my mind. I was thinking how to convert that into a list. You're a legend mate! Thank you.
– Bharath
Jan 2 at 23:53
1
1
@Bharath No worries man. Your question was clear and understandable, and I immediately thought showing a list example would be beneficial to you. When good questions are asked, good answers always come :).
– RoadRunner
Jan 3 at 6:24
@Bharath No worries man. Your question was clear and understandable, and I immediately thought showing a list example would be beneficial to you. When good questions are asked, good answers always come :).
– RoadRunner
Jan 3 at 6:24
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54001108%2floop-through-a-complicated-xml-data-to-get-specific-values-using-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown