【问题标题】:Python and products file XMLPython 和产品文件 XML
【发布时间】:2018-02-05 16:10:45
【问题描述】:

我正在使用 python,我需要为每次出现 sku 找到 skumin-order-qtystep-quantity

输入文件是:

<product sku="1235997403">
  <sku>1235997403</sku>
  <name xml:lang="fr-FR">Huile pour entretien des destructeurs de documents HSM</name>
  <short-description xml:lang="fr-FR">Flacon 250 ml. Colis de 1 flacon.</short-description>
  <category-links>
    <category-link name="20319647o.rjpf_20320074o.rjpf" domain="RAJA-FR-WEB-0092-21" default = "1" hotdeal = "0"/>
  </category-links>
  <online>1</online>
  <quantity unit="pcs">
    <min-order-quantity>1</min-order-quantity>
    <step-quantity>1</step-quantity>
  </quantity>
....
</product>
....

我尝试使用 lxml,但未能获得最小订单数量和步骤数量

from lxml import etree
tree = etree.parse('./ST2CleanCourt.xml')
elem = tree.getroot()
for  child in elem:
        print (child.attrib["sku"]) 

我尝试使用以下两种解决方案。它有效,但我需要读取文件,所以我写了

from lxml import etree
import codecs
f=codecs.open('./ST2CleanCourt.xml','r','utf-8')
fichier = f.read()
tree = etree.fromstring(fichier)

for child in tree:
    print ('sku:', child.attrib['sku'])
    print ('min:', child.find('.//min-order-quantity').text)

我总是得到这个错误 print ('min:', child.find('.//min-order-quantity').text) AttributeError: 'NoneType' 对象没有属性 'text'

怎么了?

【问题讨论】:

  • 向我们展示您正在使用的代码。
  • &lt;product&gt; 是您的根元素。它没有具有 sku 属性的子元素。它确实有一个带有 标记名称 xml 的子元素,但标记名称与属性不同。 (即&lt;foo sku="bar"&gt;&lt;sku&gt;bar&lt;/sku&gt;

标签: python xml


【解决方案1】:

您可以使用 xpath 方法来获取所需的值。

示例:

from lxml import etree

a = """<product sku="1235997403">
  <sku>1235997403</sku>
  <name xml:lang="fr-FR">Huile pour entretien des destructeurs de documents HSM</name>
  <short-description xml:lang="fr-FR">Flacon 250 ml. Colis de 1 flacon.</short-description>
  <category-links>
    <category-link name="20319647o.rjpf_20320074o.rjpf" domain="RAJA-FR-WEB-0092-21" default = "1" hotdeal = "0"/>
  </category-links>
  <online>1</online>
  <quantity unit="pcs">
    <min-order-quantity>1</min-order-quantity>
    <step-quantity>1</step-quantity>
  </quantity>
</product>
"""

tree = etree.fromstring(a)
tags = tree.xpath('/product')

for b in tags:
    print b.attrib["sku"]
    min_order = b.xpath("//quantity/min-order-quantity")
    print min_order[0].text
    step_quality = b.xpath("//quantity/step-quantity")
    print step_quality[0].text

输出:

1235997403
1
1

【讨论】:

    【解决方案2】:

    使用超过 1 个产品和产品的根节点,您可以找到:

    x = """
    <products>
    <product sku="1235997403">
      <sku>1235997403</sku>
      <name xml:lang="fr-FR">Huile pour entretien des destructeurs de documents HSM</name>
      <short-description xml:lang="fr-FR">Flacon 250 ml. Colis de 1 flacon.</short-description>
      <category-links>
        <category-link name="20319647o.rjpf_20320074o.rjpf" domain="RAJA-FR-WEB-0092-21" default = "1" hotdeal = "0"/>
      </category-links>
      <online>1</online>
      <quantity unit="pcs">
        <min-order-quantity>1</min-order-quantity>
        <step-quantity>1</step-quantity>
      </quantity>
    </product>
    <product sku="997403">
      <sku>1235997403</sku>
      <name xml:lang="fr-FR">Huile pour entretien des destructeurs de documents HSM</name>
      <short-description xml:lang="fr-FR">Flacon 250 ml. Colis de 1 flacon.</short-description>
      <category-links>
        <category-link name="20319647o.rjpf_20320074o.rjpf" domain="RAJA-FR-WEB-0092-21" default = "1" hotdeal = "0"/>
      </category-links>
      <online>1</online>
      <quantity unit="pcs">
        <min-order-quantity>5</min-order-quantity>
        <step-quantity>7</step-quantity>
      </quantity>
    </product>
    </products>
    """
    
    from lxml import etree
    
    tree = etree.fromstring(x) 
    for  child in tree:
        print ("sku:", child.attrib["sku"])
        print ("min:", child.find(".//min-order-quantity").text)  # looks for  node below
        print ("step:" ,child.find(".//step-quantity").text)  # child with the given name
    

    本质上,您会查找具有正确名称的子节点下方的任何节点并打印其文本。

    输出:

    sku:1235997403
    min:1
    step:1
    sku:997403
    min:5
    step:7 
    

    独库:http://lxml.de/tutorial.html#elementpath

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      相关资源
      最近更新 更多