繁体   English   中英

如何使用python logging.info模块在文件中写入“列表数据”

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

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