我想让我的python将输出写入到csv文件吗?

时间:2019-05-20 11:02:23

标签: python csv

我的输出出现带有引号的列。我无法正确写入csv文件。

这是我的输出:

https://imgur.com/a/XKLbmKg

这是输出的代码:

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

1 个答案:

答案 0 :(得分:2)

您可以简单地执行以下操作:

import csv

with open('employee_file.csv', mode='w') as employee_file:
    employee_writer = csv.writer(employee_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL)

    employee_writer.writerow(['John Smith', 'Accounting', 'November'])
    employee_writer.writerow(['Erica Meyers', 'IT', 'March'])

这将输出一个employee_file.csv,其中包含

John Smith,Accounting,November
Erica Meyers,IT,March