2015-09-03 190 views
0

我一直在使用此脚本(Merge multiple XML files from command line)在Debian Jessie上合并xml文件。我正在转向运行CENTOS 6.6 x86_64的专用服务器。我有问题让脚本在新环境中工作。任何帮助,将不胜感激。合并多个XML文件

蟒版本2.7

脚本

#!/usr/bin/env python 
import sys 
from xml.etree import ElementTree 

def run(files): 
    first = None 
    for filename in files: 
     data = ElementTree.parse(filename).getroot() 
     if first is None: 
      first = data 
     else: 
      first.extend(data) 
    if first is not None: 
     print ElementTree.tostring(first) 

if __name__ == "__main__": 
    run(sys.argv[1:]) 

THE ERRORS

0+0 records in 
0+0 records out 
0 bytes (0 B) copied, 9.8139e-05 s, 0.0 kB/s 
0+0 records in 
0+0 records out 
0 bytes (0 B) copied, 7.7816e-05 s, 0.0 kB/s 
0+0 records in 
0+0 records out 
0 bytes (0 B) copied, 7.3015e-05 s, 0.0 kB/s 
0+0 records in 
0+0 records out 
0 bytes (0 B) copied, 7.1727e-05 s, 0.0 kB/s 
0+0 records in 
0+0 records out 
0 bytes (0 B) copied, 7.6014e-05 s, 0.0 kB/s 
0+0 records in 
0+0 records out 
0 bytes (0 B) copied, 8.1163e-05 s, 0.0 kB/s 
Traceback (most recent call last): 
    File "/location/private/xmlcombine.py", line 17, in <module> 
    run(sys.argv[1:]) 
    File "/location/private/xmlcombine.py", line 12, in run 
    first.extend(data) 
AttributeError: _ElementInterface instance has no attribute 'extend' 
Traceback (most recent call last): 
    File "/location/private/xmlcombine.py", line 17, in <module> 
    run(sys.argv[1:]) 
    File "/location/private/xmlcombine.py", line 12, in run 
    first.extend(data) 
AttributeError: _ElementInterface instance has no attribute 'extend' 
Traceback (most recent call last): 
    File "/location/private/xmlcombine.py", line 17, in <module> 
    run(sys.argv[1:]) 
    File "/location/private/xmlcombine.py", line 12, in run 
    first.extend(data) 
AttributeError: _ElementInterface instance has no attribute 'extend' 
Process Completed 

回答

1

此错误可以通过使用python2.6的原因引起的,因为仅延伸在python 2.7出现。你确定你使用Python 2.7吗?请问你能跑

python --version 

它可以在你的系统中的蟒蛇默认版本是2.6,你需要指定正确的,而不是

#!/usr/bin/env python 
+0

谢谢,我想通了。我将xmlcombine.py设置为调用python2.7,但不是主脚本。 \t shopt -s extglob \t for $ $ PATH_VAR/*; \t do \t [-d $ f] && cd“$ f”&& python27 $ SCRIPT/xmlcombine.py!(combined).xml>“$ f”-combined.xml; \t完成; – Pop