【问题标题】:how to write system info to a spreadsheet in python如何在python中将系统信息写入电子表格
【发布时间】:2017-12-11 20:47:19
【问题描述】:

我正在尝试将系统信息写入电子表格。但是当我尝试使用我的变量时,它们会变成黑色

导入 csv 导入操作系统 导入线缓存

os.system('getmac -v > mac.txt')
os.system("wmic bios get serialnumber > serial.txt")
os.system("wmic computersystem get model > model.txt")
os.system("hostname > hostname.txt")
os.system("ipconfig > ip.txt")


open('ip1.txt','w').writelines([line for line in open('ip.txt')if 'IPv4' in line])
open('mac1.txt','w').writelines([line for line in open('mac.txt')if 'Wi-Fi' in line])
open('mac2.txt','w').writelines([line for line in open('mac.txt')if 'Ethernet' in line])

serial = linecache.getline('serial.txt', 3)
model = linecache.getline('model.txt', 3)

mac = open("mac.txt","r")
IP = open("ip1.txt","r")
mac1 = open("mac1.txt","r")
mac2 = open("mac2.txt","r")
hostname = open("hostname.txt","r")



Rmac = mac.read()
Rip = IP.read()
Rmac1 = mac1.read()
Rmac2 = mac2.read()
Rhostname = hostname.read()



myData = [[model]]

myFile = open('example2.csv', 'w')
with myFile:
    writer = csv.writer(myFile)
    writer.writerows(myData)

这只是不会将信息写入电子表格吗?我究竟做错了什么?我对编程很陌生

【问题讨论】:

    标签: python python-2.7 export-to-csv


    【解决方案1】:

    您不需要中间文件,为什么不调用您的命令并将它们的信息立即写入您的 CSV,而无需进行所有来回跳动?

    import csv
    import subprocess
    
    # get the model
    model = subprocess.check_output(["WMIC", "computersystem", "get", "model"],
                                    universal_newlines=True).strip().rsplit("\n", 1)[1]
    # get the serial
    serial = subprocess.check_output(["WMIC", "bios", "get", "serialnumber"],
                                     universal_newlines=True).strip().rsplit("\n", 1)[1]
    # get the host name
    hostname = subprocess.check_output(["hostname"], universal_newlines=True).strip()
    
    # get WMI output for all addresses
    ips = subprocess.check_output(["WMIC", "NICCONFIG", "where", "IPEnabled=true",
                                   "get", "IPAddress"],
                                  universal_newlines=True).strip().split("\n\n")[1:]
    # post-process to get the addresses only
    ips = [ip.split(",")[0].strip('"{} ') for ip in ips]
    
    # etc.
    
    with open("example2.csv", "wb") as f:  # open your CSV for writing
        writer = csv.writer(f)  # create a writer
        # you didn't write a header but let's add it in
        writer.writerow(["model", "serial", "hostname", "ips"])  # etc., you get the picture...
        writer.writerow([model, serial, hostname, ",".join(ips)])  # add other columns, too
    

    你会得到一个不错的example2.csv,其中包含:

    型号、序列号、主机名、ips
    你的型号,你的序列号,你的主机名,List.Of.IP.Addresses

    对其他字段执行相同操作即可完成。

    【讨论】:

    • 我用这个怎么样? open('ip1.txt','w').writelines([line for line in open('ip.txt')if 'IPv4' in line])
    • @demetriwolf - 检查更新,它会从 WMI 解析出 IP 地址
    • 所以必须找到确切的命令才能输出让我们说wifi和以太网mac地址?我可以看到它可以用 wmic 完成,但它不会告诉我是什么?
    • @demetriwolf - 差不多,这些都是通用命令,您可以从中提取您感兴趣的数据。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-02
    • 2021-04-05
    • 1970-01-01
    • 2022-07-13
    • 1970-01-01
    • 2015-08-08
    相关资源
    最近更新 更多