首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将输出打印到Excel

将输出打印到Excel
EN

Stack Overflow用户
提问于 2017-02-10 19:24:28
回答 2查看 1.1K关注 0票数 1

我在格式化以下代码的输出时遇到问题:

代码语言:javascript
复制
import sys
import time
import select
import paramiko
import openpyxl

ip='address'
port='port'
username='uname'
password='pass'

cmd="wstalist | grep -w 'mac\|hostname\|tx\|rx\|lastip\|platform\|distance\|signal'"

ssh=paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip,port,username,password)

stdin,stdout,stderr=ssh.exec_command(cmd)
outlines=stdout.readlines()
resp=''.join(outlines)
print(resp)

当前输出完全如下:

代码语言:javascript
复制
"mac": "04:18:D6:96:7C:51",
    "lastip": "10.1.63.143",
    "tx": 130.0,
    "rx": 39.0,
    "signal": -46,
    "distance": 450,
            "signal": -44,
            "hostname": "Mcdonalds kenako",
            "platform": "PowerBeam M5 400",
            "signal": -44,
    "mac": "80:2A:A8:0E:A3:1E",
    "lastip": "10.1.63.190",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -46,
    "distance": 450,
            "signal": -42,
            "hostname": "Lear sewing PE",
            "platform": "PowerBeam M5 400",
            "signal": -42,
    "mac": "80:2A:A8:7C:9A:C2",
    "lastip": "10.1.63.189",
    "tx": 130.0,
    "rx": 117.0,
    "signal": -62,
    "distance": 4950,
            "signal": -60,
            "hostname": "Sandoll Consultant",
            "platform": "PowerBeam M5 400",
            "signal": -58,
    "mac": "44:D9:E7:56:E6:CB",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -63,
    "distance": 4200,
            "signal": -58,
            "hostname": "Louise Swart",
            "platform": "PowerBeam M5 400",
            "signal": -57,
    "mac": "04:18:D6:9A:5A:86",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -58,
    "distance": 3900,
            "signal": -62,
    "mac": "44:D9:E7:6A:26:B7",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -64,
    "distance": 6750,
            "signal": -62,
            "hostname": "Larry Hunt",
            "platform": "PowerBeam M5 400",
            "signal": -62,
    "mac": "04:18:D6:98:CA:7F",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -59,
    "distance": 6000,
            "signal": -60,
            "hostname": "Louis Nel",
            "platform": "PowerBeam M5 400",
            "signal": -59,
    "mac": "80:2A:A8:7C:8D:EF",
    "lastip": "0.0.0.0",
    "tx": 117.0,
    "rx": 130.0,
    "signal": -63,
    "distance": 6900,
            "signal": -60,
            "hostname": "Florence Naidoo",
            "platform": "PowerBeam M5 400",
            "signal": -59,
    "mac": "80:2A:A8:74:C0:AB",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -47,
    "distance": 300,
            "signal": -47,
            "hostname": "ETC",
            "platform": "PowerBeam M5 400",
            "signal": -43,
    "mac": "80:2A:A8:08:CB:20",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 104.0,
    "signal": -66,
    "distance": 6750,
            "signal": -63,
            "hostname": "Nicholis Lourens",
            "platform": "PowerBeam M5 400",
            "signal": -60,
    "mac": "00:27:22:56:EC:35",
    "lastip": "10.1.63.141",
    "tx": 65.0,
    "rx": 65.0,
    "signal": -62,
    "distance": 5400,
            "signal": -62,
            "hostname": "Africape",
            "platform": "AirGrid M5 HP",
            "signal": -60,
    "mac": "44:D9:E7:2A:43:00",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -59,
    "distance": 3000,
            "signal": -56,
            "hostname": "Ruth Pedzisai",
            "platform": "PowerBeam M5 400",
            "signal": -55,
    "mac": "44:D9:E7:DA:51:28",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -63,
    "distance": 3450,
            "signal": -60,
            "hostname": "Thandeka Tonjeni",
            "platform": "PowerBeam M5 400",
            "signal": -60,
    "mac": "24:A4:3C:90:2F:9F",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -62,
    "distance": 5400,
            "signal": -61,
            "hostname": "WK Construction",
            "platform": "PowerBeam M5 400",
            "signal": -59,
    "mac": "04:18:D6:38:65:01",
    "lastip": "0.0.0.0",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -47,
    "distance": 2100,
            "signal": -48,
            "hostname": "OLDBRONCO",
            "platform": "PowerBeam M5 400",
            "signal": -48,

您在输出上看到的缩进与它在out命令中的显示方式完全相同。我想提到它,以防它会成为下面的一个因素:

1)上面是单个客户端信息的片段...我需要关于删除"signal“条目的第二个和第三个实例的指导

2)如果有帮助,我希望输出如下所示,或者(我真正想做的是)将信息导出到Excel电子表格中:

...Desired输出格式:

代码语言:javascript
复制
Station MAC         Device Name     IP Address      Signal      Distance     Tx/Rx
00:27:22:54:CB:5E   Deon Nelson     192.168.10.1    -66         1950"         144.0/144.0

FYI...This命令在Ubiquity无线设备上运行

我要提前感谢你!

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-02-10 20:58:59

对CSV文件进行一些格式化和编写就足够了。我修改了它,现在可以处理多个输入...我选择"mac"作为分隔符密钥。

代码语言:javascript
复制
input_str = """\
"mac": "04:18:D6:96:7C:51",
    "lastip": "10.1.63.143",
    "tx": 130.0,
    "rx": 39.0,
    "signal": -46,
    "distance": 450,
            "signal": -44,
            "hostname": "Mcdonalds kenako",
            "platform": "PowerBeam M5 400",
            "signal": -44,
    "mac": "80:2A:A8:0E:A3:1E",
    "lastip": "10.1.63.190",
    "tx": 130.0,
    "rx": 130.0,
    "signal": -46,
    "distance": 450,
            "signal": -42,
            "hostname": "Lear sewing PE",
            "platform": "PowerBeam M5 400",
            "signal": -42,
    "mac": "80:2A:A8:7C:9A:C2",
    "lastip": "10.1.63.189",
    "tx": 130.0,
    "rx": 117.0,
    "signal": -62,
    "distance": 4950,
            "signal": -60,
            "hostname": "Sandoll Consultant",
            "platform": "PowerBeam M5 400",
            "signal": -58,"""

input_list = [x.strip() for x in input_str.split(",\n")]  # split and strip indentations
input_list = input_list[::-1]  # reverse list to ignore duplicating keys later

# http://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary
import ast

input_list_of_dict = ["{" + x + "}" for x in input_list]
parsed_list_of_dict = []
list_seperator_key = "mac"
input_dict = {}
for i in range(len(input_list_of_dict)):
    input_dict.update(ast.literal_eval(input_list_of_dict[i]))
    if list_seperator_key in input_dict:
        parsed_list_of_dict.append(input_dict)
        input_dict = {}

parsed_list_of_dict = parsed_list_of_dict[::-1]  # reverse list again


# writes a CSV file
header = "Station MAC,Device Name,IP Address,Signal,Distance,Tx/Rx\n"

with open("foo.csv", "w") as f:
    f.write(header)

    for i in range(len(parsed_list_of_dict)):
        f.write(parsed_list_of_dict[i]['mac'] + ",")
        f.write(parsed_list_of_dict[i]['hostname'] + ",")
        f.write(parsed_list_of_dict[i]['lastip'] + ",")
        f.write(str(parsed_list_of_dict[i]['signal']) + ",")
        f.write(str(parsed_list_of_dict[i]['distance']) + ",")
        f.write(str(parsed_list_of_dict[i]['tx']) + "/" + str(parsed_list_of_dict[i]['rx']) + "\n")   

现在使用Excel打开foo.csv

票数 0
EN

Stack Overflow用户

发布于 2017-02-10 19:35:32

我使用Excel库创建XlsxWriter文件:

请查看网页内容点击中的文档。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42158214

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档