'没有这样的孩子:pyval'错误

时间:2015-10-27 00:52:34

标签: python python-2.7 xml-parsing lxml

我正在编写一本书(Python for Data Analysis),其中包含以下代码。我在运行循环时收到错误'没有这样的孩子:pyval'。我是否有语法错误或类似的东西?

from lxml import objectify
path = 'Performance_MNR.xml'
parsed = objectify.parse(open(path))
root = parsed.getroot()
data = []
skip_fields = ['PARENT_SEQ', 'INDICATOR_SEQ', 'DESIRED_CHANGE', 'DECIMAL_PLACES']
for elt in root.INDICATOR:
    el_data = {}
    for child in elt.getchildren():
        if child.tag in skip_fields:
            continue
        el_data[child.tag] = child.pyval
        data.append(el_data)

回溯如下:

AttributeError                            Traceback (most recent call last)
<ipython-input-17-88720283f598> in <module>()
      4        if child.tag in skip_fields:
      5            continue
----> 6        el_data[child.tag] = child.pyval
      7    data.append(el_data)
      8 

lxml.objectify.pyx in lxml.objectify.ObjectifiedElement.__getattr__ (src/lxml/lxml.objectify.c:3497)()

lxml.objectify.pyx in lxml.objectify._lookupChildOrRaise (src/lxml/lxml.objectify.c:5947)()

AttributeError: no such child: pyval

4 个答案:

答案 0 :(得分:1)

我也在读这本书并遇到同样的问题。这对我有用。

skip_fields = ['PARENT_SEQ', 'INDICATOR_SEQ',
           'DESIRED_CHANGE', 'DECIMAL_PLACES', 'YEAR']

答案 1 :(得分:0)

试试这个:

for elt in root:
el_data = {}
for child in elt.getchildren():
    if child.tag in skip_fields:
        continue
    el_data[child.tag] = child.pyval
    data.append(el_data)

答案 2 :(得分:0)

尝试更改&#39; pyval&#39;进入&#39; text&#39;,那就没问题了。

data=[]
skip_fields=['PARENT_SEQ','INDICATOR_SEQ','DESIRED_CHANGE','DECIMAL_PLACES']
for elt in root.INDICATOR:
    el_data={}
    for child in elt.getchildren():
        if child.tag in skip_fields:
            continue
        el_data[child.tag]=child.text
    data.append(el_data)

答案 3 :(得分:0)

.pyval尝试返回最合适的python类型,而.text返回本机python字符串

“ Python for Data Analysis”一书中的文件可以从中下载

https://github.com/wesm/pydata-book/blob/2nd-edition/datasets/mta_perf/Performance_MNR.xml

或(原始)

https://raw.githubusercontent.com/wesm/pydata-book/2nd-edition/datasets/mta_perf/Performance_MNR.xml

代码(python 3.6)

from lxml import objectify
dataUrl = 'https://raw.githubusercontent.com/wesm/pydata-book/2nd-edition/datasets/mta_perf/Performance_MNR.xml'
from urllib.request import urlopen
retrievedString = urlopen(dataUrl).read().decode('iso-8859-1').encode('utf-8')


# XML() returns directly the root and not an element tree
parsed = objectify.XML(retrievedString)
# root = parsed.getroot()
data = []
skip_fields = ['PARENT_SEQ', 'INDICATOR_SEQ', 'DESIRED_CHANGE', 'DECIMAL_PLACES']
for elt in root.INDICATOR:
  el_data = {}
  for child in elt.getchildren():
    if child.tag in skip_fields:
      continue
    el_data[child.tag] = child.pyval
  data.append(el_data)

import pandas as pd
result = pd.DataFrame(data)

似乎可以正常工作。列的数据类型可以通过

查看
result.columns.to_series().groupby(result.dtypes).groups

=> {dtype('int64'):索引(['PERIOD_MONTH','PERIOD_YEAR'],dtype ='object'),dtype('O'):索引(['AGENCY_NAME','CATEGORY', “ DESCRIPTION”,“ FREQUENCY”,“ INDICATOR_NAME”,        'INDICATOR_UNIT','MONTHLY_ACTUAL','MONTHLY_TARGET','YTD_ACTUAL',        'YTD_TARGET'],       dtype ='object')}

也可以用.text替换.pyval,但是数据类型会明显改变

=> {dtype('O'):索引(['AGENCY_NAME','CATEGORY','DESCRIPTION','FREQUENCY','INDICATOR_NAME',        'INDICATOR_UNIT','MONTHLY_ACTUAL','MONTHLY_TARGET','PERIOD_MONTH',        'PERIOD_YEAR','YTD_ACTUAL','YTD_TARGET'],       dtype ='object')}