我需要从 Rosreestr KPT 的 xml 中在一列中显示所有具有坐标的地籍编号,第二列和第三列是坐标本身(x 和 y)。接下来是代码:
from bs4 import BeautifulSoup
from pathlib import Path
import pandas as pd
def ordinates(ordinates):
ordinate_list = []
for x in ordinates:
cad_number = [s.find('cad_number').text for s in x.find_all('common_data')
# for ordinate in x.find_all('ordinate'):
xx = [cad.text for cad in x.find_all('x')]
yy = [cad.text for cad in x.find_all('y')]
ordinate_list.extend([(d,f,a) for d,f,a in zip(cad_number,xx,yy)])
return ordinate_list
for path in Path(file_path).rglob('*.XML'):
with open(path, 'r', encoding='utf-8') as parse:
xml = parse.read()
soup = BeautifulSoup(xml, features="lxml")
ordinate = ordinates(soup.find_all('base_data'))
for k in ordinate:
data = {'Кадастровый номер':k[0],
'Координаты по X': k[2],
'Координаты по Y': k[2]}
显示以下信息(1个地籍编号和1个x和y坐标,尽管这些坐标的地籍编号可能有,例如9个)。然后是以下版本的代码:
def ordinates(ordinates):
ordinate_list = []
value_list_1 = []
for x in ordinates:
cad_number = ([s.find('cad_number').text for s in x.find_all('common_data')])
value_list = ([s.find('value').text for s in x.find_all('common_data')])
value_list_1.extend([(v,b) for v,b in zip(cad_number,value_list)])
for ordinate in x.find_all('ordinate'):
xx = [cad.text for cad in ordinate.find_all('x')]
yy = [cad.text for cad in ordinate.find_all('y')]
ordinate_list.extend([(d,f) for d,f in zip(xx,yy)])
return ordinate_list
for path in Path(file_path).rglob('*.XML'):
with open(path, 'r', encoding='utf-8') as parse:
xml = parse.read()
soup = BeautifulSoup(xml, features="lxml")
ordinate = ordinates(soup.find_all('base_data'))
for k in ordinate:
data = {'Кадастровый номер':k[0],
'Вид объекта':k[1]}
这里 def ordords 函数显示所有坐标(如果你输入 return itude_list)或所有地籍数字(如果你输入 return value_list_1),但是如何同时输出所有内容并将其写入:
data = {'Кадастровый номер':k[0],
'Координаты по X': k[2],
'Координаты по Y': k[2]}
链接到 xml 格式的 CBT
我不得不仔细地再次重写它......结果是我想要的))这里是代码: