Помогите пожалуйста разобраться, почему затирает сведения?

есть такой XML:

<common_data>
    <type>
      <code>002001001000</code>
      <value>Земельный участок</value>
    </type>
    <cad_number>52:40:0902001:102</cad_number>
    <type>
      <code>002001001000</code>
      <value>ЗУ</value>
    </type>
    <cad_number>52:45:0502000:1001</cad_number>
</common_data>

Нужно выгрузить в таблицу cad_number (колонка "КН") и value (колонка "Тип") по порядку:

          КН                Тип
0 52:40:0902001:102  Земельный участок
1 52:45:0502000:1001 ЗУ

Вот мой код:

from bs4 import BeautifulSoup
from pathlib import Path
import pandas as pd

file_path = r'C:\Users\shirshov\Desktop\11\1' # путь к xml файлам, откуда берутся данные
excel_file = r'C:\Users\shirshov\Desktop\11\1.xlsx' # Путь до excel файла, куда попадает информация из выписок


name_3 = []
name_2 = []
page_1 = pd.DataFrame(columns = [])

for path in Path(r'C:\Users\shirshov\Desktop\11\1').rglob('*.XML'):
    with open(path, 'r',encoding='utf-8') as parse: 
        xml = parse.read()
        soup = BeautifulSoup(xml, 'lxml')
        for right in soup.find_all('common_data'):
            for child_3 in right.findChildren(['type'], recursive = False):
                for child_inn in child_3.findChildren(['value'], recursive = False): 
                    for df in child_inn:
                        name_3.append(df.text)
                        

        for right_1 in soup.find_all('common_data'):
            for child_2 in right_1.findChildren(['cad_number'], recursive = False):
                    for dfgkj in child_2:
                        name_2.append(dfgkj.text)
                        

        
        for i, x in enumerate(name_2):
            fggg = '{b:6s}'.format(a=i+1,b=x)
        for i, x in enumerate(name_3):
            fggg_1 = '{b:6s}'.format(a=i+1,b=x)
            Cad_num_flat = {'КН':f'{fggg}',
                    'Тип':f'{fggg_1}',}
        
            page_1 = pd.concat([page_1, pd.DataFrame.from_records([Cad_num_flat])], ignore_index=True)
            print(page_1)


with pd.ExcelWriter(excel_file, engine='openpyxl', mode = 'w') as writer:
    page_1.to_excel(writer, sheet_name='Помещения',index=True)

В чем ошибка, где она? Помогите пожалуйста.


Ответы (1 шт):

Автор решения: Михаил Ширшов

Получилось, вот таким способом

from bs4 import BeautifulSoup
from pathlib import Path
import pandas as pd

file_path = r'C:\Users\shirshov\Desktop\11\1' # путь к xml файлам, откуда берутся данные
excel_file = r'C:\Users\shirshov\Desktop\11\1.xlsx' # Путь до excel файла, куда попадает информация из выписок


name_3 = []
name_2 = []
page_1 = pd.DataFrame(columns = [])

for path in Path(r'C:\Users\shirshov\Desktop\11\1').rglob('*.XML'):
    with open(path, 'r',encoding='utf-8') as parse: 
        xml = parse.read()
        soup = BeautifulSoup(xml, 'lxml')
        for right in soup.find_all('common_data'):
            for child_3 in right.findChildren(['type'], recursive = False):
                for child_inn in child_3.findChildren(['value'], recursive = False): 
                    for df in child_inn:
                        name_3.append(df.text)
                        

        for right_1 in soup.find_all('common_data'):
            for child_2 in right_1.findChildren(['cad_number'], recursive = False):
                    for dfgkj in child_2:
                        name_2.append(dfgkj.text)
                        
        
        
        for f, n in zip(name_2,name_3):
            Cad_num_flat = {'КН':f'{f}',
                            'Тип':f'{n}',}
            page_1 = pd.concat([page_1, pd.DataFrame.from_records([Cad_num_flat])], ignore_index=True)
            print(f'КН:{f}')
            print(f'Тип:{n}')


with pd.ExcelWriter(excel_file, engine='openpyxl', mode = 'w') as writer:
    page_1.to_excel(writer, sheet_name='Помещения',index=True)

→ Ссылка