简体   繁体   English

递归解析所有xml文件并排除文件夹

[英]Recursively parse all xml files and exclude folder

I am trying to parse all XML files in a given folder/subfolders and search and replace text inside that XML. 我试图解析给定文件夹/子文件夹中的所有XML文件,然后搜索并替换该XML中的文本。 All while excluding the subfolder "Archive". 全部不包括子文件夹“ Archive”。 I am getting the error "AttributeError: 'NoneType' object has no attribute 'replace'" Not sure what I am missing, but my loop seems to die once it reaches the ElementTree to open and parse the XML. 我收到错误消息“ AttributeError:'NoneType'对象没有属性'replace'”不确定我丢失了什么,但是一旦到达ElementTree来打开并解析XML,我的循环似乎就死了。

for roots, dirs, files in os.walk("C:\test", topdown=True):
    if 'Archive' in dirs:
        dirs.remove('Archive')
    #dirs[:] = [d for d in dirs if 'Archive' not in d]
    for f in files:
        if f.endswith('.xml'):
            try:
                with open(os.path.join(roots, f), 'r') as xml:
                  tree = ET.parse(xml)
                  root = tree.getroot()

                  for elem in root.getiterator():
                    try:
                      print (elem.text)
                      elem.text = elem.text.replace('_THUMBNAIL.jpg', '.mxd.jpg')

                    except ET.ParseError:
                        pass

                tree.write(xml, encoding='utf-8')
            except FileNotFoundError:
                pass

I guess that not all XML tags have a text. 我猜并不是所有的XML标签都有文本。 So you should use 所以你应该使用

if elem.text is not None :
    try:
        print (elem.text)
        elem.text = elem.text.replace('_THUMBNAIL.jpg', '.mxd.jpg')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM