将输出打印到Excel

时间:2017-02-10 11:24:28

标签: python linux

我无法格式化以下代码的输出:

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)

目前输出完全如下:

"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,

你在输出上看到的缩进就像它在命令之外的显示方式一样。我想提一下它以防万一它是以下因素:

1)以上是单个客户信息的片段...我需要有关删除“信号”条目的第二个和第三个实例的指导

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

...所需的输出格式:

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 ...此命令在Ubiquity无线设备上运行

我提前谢谢你了!!!

2 个答案:

答案 0 :(得分:1)

我使用XlsxWriter lib来创建Excel文件:

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

答案 1 :(得分:0)

某些格式化和编写CSV文件就足够了。我现在修改它来处理多个输入...我选择"mac"作为分隔符键。

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