簡體   English   中英

如何從具有 null 值的 xml 文件打印記錄器?

[英]How to print logger from a xml file which have null value?

我正在嘗試從 xml 文件中捕獲一些字段。 使用“logger.info”我已經成功打印了我的代碼日志。 以下是我的代碼:

#providing the path for client fr & counting the total files processed
D_DIR = Path(directory[0])
client_id = directory[0].split(os.sep)[-2]
files = sorted(D_DIR.glob("*.xml"))
totalFiles[client_id] = len(files)
total_files += len(files)
logger.info ("Processing for client '{}'".format(clients[0]))
logger.info(f"Processing {len(files)} number of files")

#the code for capturing the neccessary data from the xml files
for i in directory:
    D_DIR = Path(i)
    files = sorted(D_DIR.glob("*.xml"))
    for file in files:
        tree = ET.parse(file)
        root = tree.getroot()  
        RowCount = 0
        for obj in root.findall("object"):
            for i in obj.findall("record"):            
                Data_Capture_Date = i.find("invoice_capture_date").text  
                Case_Id = i.find("case_id").text
                Organization = i.find("organization").text
                Supplier_number = i.find("supplier_number").text
                Invoice_Number = i.find("invoice_number").text
                Document_Type = i.find("document_type").text
                Invoice_Source = i.find("invoice_source").text
                Recieved_Date = time.ctime(os.path.getctime(file))
                for rows in root.iter("rows"):
                    RowCount =+ len(rows)
                logger.info("Data_Capture_Date:"+Data_Capture_Date + " : " +"Case_Id:" + Case_Id + " : "  +"Organization:" +Organization + " : " +"Supplier_number:" + (Supplier_number)+ " : " +"Invoice_Number:" +(Invoice_Number)+ " : " +"Document_Type:" +Document_Type+ " : " +"Invoice_Source:" +Invoice_Source+ " : " +"Recieved_Date:" + Recieved_Date + " : "+"RowCount:" + str(RowCount)) 

#insert record into activity table
if not functions.writeActivityFile('captuissue', 'success', logger.handlers[0].baseFilename, cursor):
    logger.info("Write to Activity Table Failed.")

#closing the connection
dbconn.commit()
cursor.close()
dbconn.close()
           
logger.info("Information capture issue process completed")
logger.info("Total files processed : {}".format(total_files))
logger.info("Information Capture : END")
print("Check logfile '{}' for details".format(logger.handlers[0].baseFilename))  

在這里我沒有添加記錄器啟動過程,因為它不需要。 主要過程就是上面的代碼。

當我的文件在任何字段中具有 null 值時,它會顯示錯誤。

TypeError: can only concatenate str (not "NoneType") to str

我怎樣才能在我的記錄器中也采用無類型值?

我找到了這樣做的方法。 我們需要在捕獲過程代碼下方以“.format”方式編寫記錄器。

 logger.info("data_capture_date:{} |  case_id:{} | organization:{} | supplier_number:{} | invoice_Number:{} | document_Type:{} | invoice_Source:{} |  rowcount:{}".format(data_capture_date, case_id,organization,supplier_number,invoice_Number,document_Type,invoice_Source,rowcount))

此方法在記錄器中顯示 n.netype

暫無
暫無

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

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