1

我正在使用 Sentinel-2 图像,我想从 XML 文件中检索 Cloud_Coverage_Assessment。我需要用 Python 来做这件事。

有谁知道如何做到这一点?我想我必须使用 xml.etree.ElementTree 但我不确定如何使用?

XML 文件:

<n1:Level-1C_User_Product xmlns:n1="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd">
  <n1:General_Info>
  ...
  </n1:General_Info>
  <n1:Geometric_Info>
  ...
  </n1:Geometric_Info>
  <n1:Auxiliary_Data_Info>
  ...
  </n1:Auxiliary_Data_Info>
  <n1:Quality_Indicators_Info>
    <Cloud_Coverage_Assessment>90.7287</Cloud_Coverage_Assessment>
    <Technical_Quality_Assessment>
    ...
    </Technical_Quality_Assessment>
    <Quality_Control_Checks>
    ...
    </Quality_Control_Checks>
  </n1:Quality_Indicators_Info>
</n1:Level-1C_User_Product>
4

2 回答 2

1

从文件中读取 xml

import xml.etree.ElementTree as ET
tree = ET.parse('sentinel2.xml')
root = tree.getroot()

print(root.find('.//Cloud_Coverage_Assessment').text)
于 2022-02-14T09:54:03.130 回答
0

..我想检索 Cloud_Coverage_Assessment

试试下面的(使用xpath)

import xml.etree.ElementTree as ET


xml = '''<n1:Level-1C_User_Product xmlns:n1="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://psd-14.sentinel2.eo.esa.int/PSD/User_Product_Level-1C.xsd">
  <n1:General_Info>
  </n1:General_Info>
  <n1:Geometric_Info>
  </n1:Geometric_Info>
  <n1:Auxiliary_Data_Info>
  </n1:Auxiliary_Data_Info>
  <n1:Quality_Indicators_Info>
    <Cloud_Coverage_Assessment>90.7287</Cloud_Coverage_Assessment>
    <Technical_Quality_Assessment>
    </Technical_Quality_Assessment>
    <Quality_Control_Checks>    
    </Quality_Control_Checks>
  </n1:Quality_Indicators_Info>
</n1:Level-1C_User_Product>'''


root = ET.fromstring(xml)
print(root.find('.//Cloud_Coverage_Assessment').text)

输出

90.7287
于 2022-02-11T14:23:38.387 回答