Python 我该如何改变这一点,因为他们又在一起了

Python 我该如何改变这一点,因为他们又在一起了,python,xml,Python,Xml,所以我今天尝试了另一种方法,我试图输出所有餐厅的餐厅名称和地址的XML内容, 这是我的密码 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 f

所以我今天尝试了另一种方法,我试图输出所有餐厅的餐厅名称和地址的XML内容, 这是我的密码

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 tag in root_node.findall('restaurant/address'):
        print(tag.text)
我知道可能我在这里使用了错误的代码,但我不知道如何更改它

for tag in root_node.findall('restaurant/address'):
    print(tag.text)
这是我的xml代码

<record>
   <restaurant name="La Pasteria" rate="-">
      <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="3">
      <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>
   </restaurant>
   <restaurant name="Malaya Food" rate="5">
      <cuisine id="115">Malaysian</cuisine>
      <address>22, Jalan Ayer Keroh, 7520 Melaka</address>
      <capacity>50</capacity>
      <phone>06-2723603</phone>
         <general>-</general>
         <shop1>-</shop1>
         <shop2>-</shop2>
   </restaurant>
</record>

意大利人
雪兰莪州吉兰马塔古晋8号,邮编89200
300
06-2899808
06-2829818
-
-
-
马来西亚人
马六甲75350号贾兰塔明纱丽21号
马六甲75450号,Jalan Bukit Beruang 25号
80
012-2677498
06-2855413
06-2856418
马来西亚人
马六甲7520号Jalan Ayer Keroh 22号
50
06-2723603
-
-
-

你就快到了;只需将内部循环从

for tag in root_node.findall('restaurant/address'):
        print(tag.text)

输出应该是

Restaurant name:
La Pasteria
8, Jalan Mata Kuching, 89200 Selangor
Restaurant name:
Nyonya Baba
#note: in your xml, this restaurant has two different addresses, for some reason
21, Jalan Taming Sari, 75350 Melaka
25, Jalan Bukit Beruang, 75450 Melaka
Restaurant name:
Malaya Food
22, Jalan Ayer Keroh, 7520 Melaka
 for address in tag.findall('.//address'):
        print(address.text)
Restaurant name:
La Pasteria
8, Jalan Mata Kuching, 89200 Selangor
Restaurant name:
Nyonya Baba
#note: in your xml, this restaurant has two different addresses, for some reason
21, Jalan Taming Sari, 75350 Melaka
25, Jalan Bukit Beruang, 75450 Melaka
Restaurant name:
Malaya Food
22, Jalan Ayer Keroh, 7520 Melaka