简体   繁体   English

使用python将信息从Linux文件解析到Windows

[英]Parse information from Linux file to Windows using python

I am trying to parse some contents from my Linux environment and dump them into an excel file in my Windows environment using python.我正在尝试从我的 Linux 环境中解析一些内容,并使用 python 将它们转储到我的 Windows 环境中的 excel 文件中。

eg: foo/bar/myfile.txt has some contents I want to stick to an excel file in my windows env C:foo\\bar\\myfile.txt eg: foo/bar/myfile.txt有一些我想粘贴到我的 windows 环境中的 excel 文件的内容C:foo\\bar\\myfile.txt

I know how to extract the information I need but I cannot find a solution to create a file in my Windows OS from my linux env in python.我知道如何提取我需要的信息,但我找不到在我的 Windows 操作系统中从 python 中的 linux env 创建文件的解决方案。 any little info helps.任何小信息都有帮助。 Thanks!谢谢!

import csv
import sys
import subprocess


def env_key_values(host):
    """
    The subprocess.Popen will execute the env command on the remote
    Linux server then return the results.
    """
    ssh = subprocess.Popen(["ssh", host, "env"],
                            shell=False,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE)
    """
    Read each line from stdout selecting only the varname=value lines.
    Split the line on the '=' character. Use the first element as the 
    Linux environment variable name. Use the remaining elements as the value.
    yield the result.
    """
    for line in ssh.stdout:
        str_line = line[:-1].decode("utf-8")
        if "=" in str_line:
            key_values = str_line.split("=")
            values = "=".join(key_values[1:])
            yield (key_values[0], values)

if __name__ == "__main__":
    """
    Open desired file in write mode, do not enforce EOL character.
    Create a CSV writer specifying the Windows EOL.
    Use \t character for delimiter as the specified file in the question
    used a .txt extension and the user wishes to import the file into Excel.
    """
    with open("file.txt", "w", newline=None) as fout:
        csv_out = csv.writer(fout, delimiter="\t" lineterminator="\r\n")
        csv_out.writerow(["VARNAME","VALUE"])
        for row in env_key_values("ldssbox01"):
            csv_out.writerow(row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用脚本(Linux或Windows或Python)从日志文件中提取特定信息 - Extract specific information from a log file using a script (Linux or Windows or Python) 使用 python/grep 从 Windows 和 Linux 上的二进制文件中提取字符串 - Extract string from a binary file on Windows and Linux using python/grep Python:解析刺定界文件-代码在Windows上有效,但在Linux上无效? - Python: Parse thorn delimited file - code works on Windows, but not on Linux? 我正在尝试使用来自 python 的两个用户输入来解析来自 JSON URL 文件的信息 - I am trying to parse information from a JSON URL file using two user inputs from python 将文件从Windows主机复制到Linux [Python] - Copy File from Windows Host to Linux [Python] 如何使用 python 仅解析来自 XML 的某些信息 - How to parse only certain information from XML using python 如何将文件从 Windows 复制到 Windows/Linux uisng Python 脚本 - How to copy a file from Windows to Windows/Linux uisng Python script 使用 Python 从文本文件中提取信息 - Extracting information from a text file using Python 在Linux上通过Python解析Windows命令行 - Parse Windows command line via Python on Linux 使用Python从XML文件中提取信息? - Using Python to extract information from a XML file?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM