簡體   English   中英

從 Python 中具有相同文件擴展名的多個文件的目錄中讀取文件

[英]Read File From Directory with Multiple Files of the Same File Extension in Python

I am trying to parse a specific XSD file (File1.xsd) located in the file path shown below using Python, but there are multiple files in that file path with the same file extension (File2.xsd and File3.xsd). File1.xsd will be used to validate an xml against the File1.xsd schema, but I am unable parse the file path for just this single XSD file (per the "xml_validator" function).

Does anybody know how to tweak/enhance the code to look for only File1.xsd and otherwise apply the same technique for a directory with multiple files of the same file extension - The output will be a Boolean expression per the "is_valid" function, any非常感謝您的幫助!

import os
import glob
import lxml
from lxml import etree
from lxml.etree import XMLSchema
import xmlschema

path = "C:\\Users\\mdl518\\Desktop\\Data\\"

def validation():
    for filename in glob.glob(os.path.join(path, "*.xml")):
        with open(filename,'r') as f: 
            xml_file=lxml.etree.parse(f)
            xml_validator=lxml.etree.XMLSchema(file="<path_to_File1.xsd">))
            is_valid=xml_validator.validate(xml_file)
            print(is_valid)
        return

validation()

將 xml_validator 行替換為: xml_validator=lxml.etree.XMLSchema(os.path.join(path,filename.replace(".xml",".xsd")))

我假設你的文件名最后只包含字符串“.xml”,如果你有像“my.xmlfile.xml”這樣的文件名,你應該只替換最后一個“.xml”實例。

您可以打開您需要作為驗證器的文件( File1.xsd ),對其進行解析,然后遍歷.xml文件:

path = "C:\\Users\\mdl518\\Desktop\\Data\\"

def validation():
    with open(f"{path}File1.xsd", 'r') as filxsd:
         xml_validator = XMLSchema(file=filxsd)
    for fil in glob.glob(f"{path}*.xml"):                 
        with open(fil, 'r') as f: 
            xml_file = lxml.etree.parse(f)
            is_valid = xml_validator.validate(xml_file)
            print(is_valid)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM