在Python 2.7.9中将列表复制到剪贴板(将复杂列表转换为字符串)

时间:2018-03-07 15:56:02

标签: python string python-2.7 list

我尝试编写一个登录路由器的程序,将show log命令的输出复制到剪贴板,然后将其粘贴到文本文件或网页。我可以打印出我正在寻找的输出。这是一个示例:

.Feb 27 07:25:28.050: %OSPF-4-ERRRCV: Received invalid packet: Bad Checksum from 10.1.252.231, GigabitEthernet0/0/4

.Feb 27 07:25:38.135: %OSPF-4-ERRRCV: Received invalid packet: Bad Checksum from 10.1.252.231, GigabitEthernet0/0/4

.Feb 27 07:25:53.001: %OSPF-4-ERRRCV: Received invalid packet: Bad Checksum from 10.1.252.231, GigabitEthernet0/0/4

.Feb 27 07:26:02.746: %OSPF-4-ERRRCV: Received invalid packet: Bad Checksum from 10.1.252.231, GigabitEthernet0/0/4

.Feb 27 07:26:12.033: %OSPF-4-ERRRCV: Received invalid packet: Bad Checksum from 10.1.252.231, GigabitEthernet0/0/4

.Feb 27 07:26:21.517: %OSPF-4-ERRRCV: Received invalid packet: Bad Checksum from 10.1.252.233, GigabitEthernet0/0/4

.Feb 27 07:26:35.721: %OSPF-4-ERRRCV: Received invalid packet: Bad Checksum from 10.1.252.233, GigabitEthernet0/0/4

但是,我将其复制到剪贴板的代码段返回以下错误消息:

TypeError: GTK.Clipboard.set_text() argument 1 must be string, not list

有关如何将输出转换为字符串数据类型的任何建议?

3 个答案:

答案 0 :(得分:1)

看起来你有一个字符串列表必须转换为单个字符串。假设变量被称为strings,那么你需要做类似的事情:

... set_text("\n".join(strings))

答案 1 :(得分:0)

添加以下代码行解决了问题并成功将“show log”输出复制到剪贴板:pyperclip.copy(“\ n”.join(Log))

答案 2 :(得分:0)

  

关于如何将输出转换为字符串数据类型的任何建议?

使用join可以将列表转换为字符串。