使用python输出xml
昨天,关于使用python输出xml,我问了类似的问题。但是今天,我想使用python代码显示餐厅名称的XML内容以及大于4的容量。这是我的代码使用python输出xml,python,xml,Python,Xml,昨天,关于使用python输出xml,我问了类似的问题。但是今天,我想使用python代码显示餐厅名称的XML内容以及大于4的容量。这是我的代码 <record> <restaurant name="La Pasteria" rate="3"> <cuisine id="-">Italian</cuisine> <address>8, Jalan
<record>
<restaurant name="La Pasteria" rate="3">
<cuisine id="-">Italian</cuisine>
<address>8, Jalan Mata Kuching, 89200 Selangor</address>
<capacity>300</capacity>
<phone>06-2899808</phone>
<phone>06-2829818</phone>
<general>-</general>
<shop1>-</shop1>
<shop2>-</shop2>
</restaurant>
<restaurant name="Nyonya Baba" rate="5">
<cuisine id="112">Malaysian</cuisine>
<address>21, Jalan Taming Sari, 75350 Melaka</address>
<address>25, Jalan Bukit Beruang, 75450 Melaka</address>
<capacity>80</capacity>
<phone>
<general>012-2677498</general>
<shop1>06-2855413</shop1>
<shop2>06-2856418</shop2>
</phone>
</record>
意大利人
雪兰莪州吉兰马塔古晋8号,邮编89200
300
06-2899808
06-2829818
-
-
-
马来西亚人
马六甲75350号贾兰塔明纱丽21号
马六甲75450号,Jalan Bukit Beruang 25号
80
012-2677498
06-2855413
06-2856418
下面是我在python中使用的内容
import xml.etree.ElementTree as ET
# We're at the root node (<page>)
root_node = ET.parse('record.xml').getroot()
# We need to go one level below to get <items>
# and then one more level from that to go to <item>
for tag in root_node.findall('restaurant'):
value = tag.attrib['name']
print("Restaurant name:")
print(value)
for capacity in tag.findall('capacity'):
print(capacity.text)
将xml.etree.ElementTree作为ET导入
#我们在根节点()
root_node=ET.parse('record.xml').getroot()
#我们需要到下一层才能得到
#然后再从这一层转到另一层
对于根节点findall('restaurant')中的标记:
value=tag.attrib['name']
打印(“餐厅名称:”)
打印(值)
对于tag.findall中的容量(“容量”):
打印(容量.文本)
如果餐厅价格大于4,我需要在代码中添加什么来输出?您所要做的就是检查价格是否大于4
import xml.etree.ElementTree as ET
# We're at the root node (<page>)
root_node = ET.parse('record.xml').getroot()
# We need to go one level below to get <items>
# and then one more level from that to go to <item>
for tag in root_node.findall('restaurant'):
if int(tag.attrib.get("rate", 0)) > 4:
value = tag.attrib['name']
print("Restaurant name:")
print(value)
for capacity in tag.findall('capacity'):
print(capacity.text)
将xml.etree.ElementTree作为ET导入
#我们在根节点()
root_node=ET.parse('record.xml').getroot()
#我们需要到下一层才能得到
#然后再从这一层转到另一层
对于根节点findall('restaurant')中的标记:
如果int(tag.attrib.get(“rate”,0))>4:
value=tag.attrib['name']
打印(“餐厅名称:”)
打印(值)
对于tag.findall中的容量(“容量”):
打印(容量.文本)