如何使用xml Element etree在循环遍历根时获取每个子节点的索引?

时间:2016-03-11 20:08:15

标签: python xml lxml elementtree xml.etree

这是我的代码:

X_train = pd.read_csv('data/train_features.txt',header=0,names=['area','length','index','complexity','lines','curves','intensity'])
y_train = pd.read_csv('labels/train_labels.txt',header=0,names=['r','g','b'])

l_model = linear_model.LinearRegression()
l_model.fit(X_train,y_train)

X_test = pd.read_csv('data/test_features.txt',header=0,names=['area','length','index','complexity','lines','curves','intensity'])

with open('test.svg','rwb') as o:
    tree = ET.parse(o)
    root = tree.getroot()
    for index,child in enumerate(root):
        features = np.array(X_test.loc[index,:])
        child.set('fill',l_model.predict(features))
    tree.write('best_guess.svg')

正如您所看到的,我正在尝试遍历我的svg文件的根元素中的每个子元素,并最终使用一个预测我的线性模型设置fill元素。

然而,在发生任何这种情况之前,我很难得到这个错误,我相信因为我的for循环没有正确获得索引 - 换句话说,我不认为你可以做这个枚举技巧使用和svg的根元素就像你可以使用python数组一样。这是错误:

Traceback (most recent call last):
  File "guess.py", line 26, in <module>
    features = np.array(X_test.loc[index,:])
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1225, in __getitem__
    return self._getitem_tuple(key)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 738, in _getitem_tuple
    return self._getitem_lowerdim(tup)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 863, in _getitem_lowerdim
    section = self._getitem_axis(key, axis=i)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1371, in _getitem_axis
    self._has_valid_type(key, axis)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1333, in _has_valid_type
    error()
  File "/usr/local/lib/python2.7/site-packages/pandas/core/indexing.py", line 1320, in error
    (key, self.obj._get_axis_name(axis)))
KeyError: 'the label [2809] is not in the [index]' 
似乎不喜欢我调用索引。所以是的,我怎样才能在获取索引的同时遍历根?

0 个答案:

没有答案