[英]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.