如何将具有列的txt文件保存到csv文件中?

时间:2019-05-20 12:33:12

标签: python csv

我将我在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"

3 个答案:

答案 0 :(得分:2)

您似乎导入了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

答案 1 :(得分:1)

换行写

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

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

'\ n'-移至下一行。

答案 2 :(得分:0)

还请查看net-snmp的output formats,以使解析更加轻松可靠。