我真的不知道如何在标题中解释这一点。问题是,我查找文本通道中的所有消息并将其填充到列表中,然后将其保存到文本文件中。由于在聊天客户端中形成,消息的内容具有新行。聊天记录器只保存带有换行符的所有消息,从而损害日志文件。
聊天客户端输入
user1: who are the users on this server?
user2: oh that' easy my name is harry
user3 is adam
user4 is max
user1: okay got that中断聊天记录器输出
12345 user1: who are the users on this server?
23456 user2: oh that' easy my name is harry
user3 is adam
user4 is max
12345 user1: okay got that我想要实现的
12345 user1: who are the users on this server?
23456 user2: oh that' easy my name is harry
23456 user2: user3 is adam
23456 user2: user4 is max
12345 user1: okay got that或
12345 user1: who are the users on this server?
23456 user2: oh that' easy my name is harry user3 is adam user4 is max
12345 user1: okay got that有什么窍门可以实现吗?我可以做string.replace('\n',‘'),但是我对我建议的第一个方法更感兴趣。
发布于 2020-08-16 19:22:36
第三个选项如何,使用日志中字符串的repr版本:
messages = [
(12345, 'user1: who are the users on this server?'),
(23456, '''user2: oh that' easy my name is harry
user3 is adam
user4 is max'''),
(12345, 'user1: okay got that'),
]
for x, msg in messages:
print(x, repr(msg))输出:
12345 'user1: who are the users on this server?'
23456 "user2: oh that' easy my name is harry\nuser3 is adam\nuser4 is max"
12345 'user1: okay got that'这样您就不必实现像第一个示例输出那样的特殊格式,也不必丢失像第二个示例输出中的信息(将换行符压缩到空格中)。
https://stackoverflow.com/questions/63440942
复制相似问题