[英]how to write 'list data' in a file using python logging.info module
我正在嘗試使用python logging.info模塊在文本文件中記錄列表數據。 我可以在我的日志文本文件中看到已記錄的數據,但是它總是將列表數據放在'('')'括號內。 我無法理解其原因。
請幫助我理解它。 如果我是python的初學者,請耐心等待我遺漏任何明顯的東西。
這是我的代碼:
therapy = "log data:",":".join ("{:02x}".format(x) for x in respList[4:])
logging.info(therapy)
date = "Date:","/".join("{:02x}".format(a) for a in ts[:-4])
logging.info(date)
time = "Time:",":".join("{:02x}".format(a) for a in ts[3:6])#-3:
logging.info(time)
這是日志文本文件中的輸出:
2019-03-04 17:31:18,943 -
('log data:','01:00:00:03:05:26:58:18:00:00:03:e8:00:00:32
:00:01:32:0e:00:c8:01:19:03:04:11:27:25:1c')
2019-03-04 17:31:18,943 - ('Date:', '19/03/04')
2019-03-04 17:31:18,943 - ('Time:', '11:27:25')
提前致謝!
您只創建了一個元組"string","string"
而不是將其污染了"string"+"string"
通過在兩個字符串之間添加一個逗號作為所有分配的值,您可以為變量分配元組,當通過記錄器時,將在其周圍加上括號以格式化,因為這是元組的__repr__
函數對其進行格式化的方式。
您可以使用+
運算符將兩個字符串連接在一起,但在冒號后面放置一個空格以提高可讀性:
therapy = "log data: " + ":".join ("{:02x}".format(x) for x in respList[4:])
logging.info(therapy)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.