如何将包含列的txt文件保存到csv文件中?

2024-07-04 16:43:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我在python上使用的终端命令的输出保存到一个txt文件中。然后我需要把那个txt文件保存到csv文件中。 问题是,当我写入csv文件时,所有内容都写入一行

这是输出代码:

import subprocess
import csv


pl = subprocess.Popen(["snmptable", "-v2c", "-c", "public", "104.236.166.95",
                       "hrSWRunTable"], stdout=subprocess.PIPE).communicate()[0]
print(pl.decode("utf-8"))
pll = pl.decode("utf-8")
text_file = open("Output.txt", "w")
text_file.write(pll)
text_file.close()

with open('Output.txt') as infile, open('coordv.csv', 'w') as outfile:
    for line in infile:
        outfile.write(" ".join(line.split()).replace(' ', ','))
        outfile.write(",")

这是我要添加到csv中的内容:

SNMP table: HOST-RESOURCES-MIB::hrSWRunTable

 hrSWRunIndex       hrSWRunName               hrSWRunID                                                        hrSWRunPath                                                                                                                  hrSWRunParameters hrSWRunType hrSWRunStatus
            1            "init" SNMPv2-SMI::zeroDotZero                                                         "init [4]"                                                                                                                                 "" application      runnable
            2     "migration/0" SNMPv2-SMI::zeroDotZero                                                      "migration/0"                                                                                                                                 "" application      runnable
            3     "ksoftirqd/0" SNMPv2-SMI::zeroDotZero                                                      "ksoftirqd/0"                                                                                                                                 "" application      runnable
            4     "migration/1" SNMPv2-SMI::zeroDotZero                                                      "migration/1"                                                                                                                                 "" application      runnable
            5     "ksoftirqd/1" SNMPv2-SMI::zeroDotZero                                                      "ksoftirqd/1"                                                                                                                                 "" application      runnable
            6        "events/0" SNMPv2-SMI::zeroDotZero                                                         "events/0"                                                                                                                                 "" application      runnable
            7        "events/1" SNMPv2-SMI::zeroDotZero                                                         "events/1"                                                                                                                                 "" application      runnable
            8         "khelper" SNMPv2-SMI::zeroDotZero                                                          "khelper"                                                                                                                                 "" application      runnable
            9         "kthread" SNMPv2-SMI::zeroDotZero                                                          "kthread"                                                                                                                                 "" application      runnable
           87       "kblockd/0" SNMPv2-SMI::zeroDotZero                                                        "kblockd/0"                                                                                                                                 "" application      runnable
           88       "kblockd/1" SNMPv2-SMI::zeroDotZero                                                        "kblockd/1"                                                                                                                                 "" application      runnable
           89          "kacpid" SNMPv2-SMI::zeroDotZero                                                           "kacpid"                                                                                                                                 "" application      runnable
          184           "ata/0" SNMPv2-SMI::zeroDotZero                                                            "ata/0"                                                                                                                                 "" application      runnable
          185           "ata/1" SNMPv2-SMI::zeroDotZero                                                            "ata/1"                                                                                                                                 "" application      runnable
          186         "ata_aux" SNMPv2-SMI::zeroDotZero                                                          "ata_aux"

Tags: 文件csvtxtapplicationeventsmigrationsmipl
3条回答

还可以查看netsnmp的output formats,以获得更简单、更可靠的解析

您似乎导入了csv模块来编写csv,但实际上并没有使用它

获取为编写而创建的文件句柄,并从中创建一个csv.writer。然后,您可以用它写行;见https://docs.python.org/3/library/csv.html#csv.writer

所以,对于你的情况:

with open('Output.txt') as infile, open('coordv.csv', 'w') as outfile:
    writer = csv.writer(outfile)
    for line in infile:
        writer.writerow(line.split())  # ... or whatever you'd need in the rows

换行

# Write initial text
outfile.write(...)

# At end of first row, move to the next line
outfile.write('\n')

'\n'-这将移到下一行

相关问题 更多 >

    热门问题