有谁知道为什么我的Python文档输出有这个错误?我使用lxml元素etree。 定义功能时出现错误 .................................................. .................................................. .................................................. .........
List all purchases of the year chosen by the user:
Buy: 2017/12/01, Delivered, Ship all over as each product is available.
Traceback (most recent call last):
File "D: \ ABC \ 1stan \ 1st Semester \ TI \ tp3 \ python \ ola.py", line
55, in <module>
printCompras (treeDoc, "2017", prefix)
File "D: \ ABC \ 1styear \ 1st Semester \ TI \ tp3 \ python \ ola.py",
line 20, in printCompras
printCompra (compraElem, prefix)
File "D: \ ABC \ 1styear \ 1st Semester \ TI \ tp3 \ python \ ola.py",
line 34, in printCompra
"+", "+ buy_id [0] .text +", "+ buy_id [0] .text +", "+ buy_id [0] .text
+ )
IndexError: list index out of range
我的Python文件:
from lxml import etree
file = "loja.xml"
treeDoc = etree.parse (file)
buyElem = treeDoc.xpath ("// purchase")
listUsers = treeDoc.xpath ("// user")
productElem = treeDoc.xpath ("// product")
characteristicaElem = treeDoc.xpath ("// product / technical_characters /
feature")
olamundo = treeDoc.xpath ("// purchase / product_bought")
print ("List all Purchases of the year chosen by the user:")
prefix = ""
def printCompras (treeDoc, year, prefix):
for purchase in compraElem:
buy_date = purchase.xpath ("purchase_date")
ano_data = (buy_date [0] .text) .split ("/")
buy_year = year_data [0]
if year == year_of_buy:
printCompra (compraElem, prefix)
def printCompra (compraElem, prefix):
for purchase in compraElem:
buyer_id = buyElem [0] .get ("buyer")
for user in listaUsers:
user_name = user.xpath ("@ user_id =" + buyer_id + "/ name")
compra_id = compra.xpath ("compra_id")
buy_date = purchase.xpath ("purchase_date")
buy_account = buy.xpath ("buy_account") [0]
indication = purchase.xpath ("indication")
for purchase in compraElem:
print (prefix + "Buy:" + buy_date [0] .text + "," + buy_date.get
("situation") + "," + indication [0] .text)
"+", "+ buy_id [0] .text +", "+ buy_id [0] .text +", "+ buy_id [0]
.text + )
(+), "+", "+", "+", "+", "+", "+", "[ )
for product in productElem:
carrier = olamundo [0] .get ("carrier")
printProduct (productName, carrier, prefix)
def printProduct (productEle, carrier, prefix):
product_id = olamundo [0] .get ("@ product_id")
product_name = treeDoc.xpath ("// buy [purchased_product / @ product_id =]")
price = treeDoc.xpath ("// product bla bla bla")
print (prefix + "Product:" + product_id + "," + name [0] .text + "," + price
[0] .text + "," +
print (prefix + "Product:" + product_id + "," + olamundo [0] .get ("@
carrier"))
def printVender ():
identifier, name, average-rating-in-stars
def printCaracteristica (characteristicElement, prefix):
name = characterElement.get ("name")
value = characterElement.get ("value")
print (prefix + "Characteristics:" + name + "-" + value)
printCompras (treeDoc, "2017", prefix)