简体   繁体   中英

I would like my python to write my output to a csv file?

The output I have appears a columns with quotes. I cant get it to properly write to a csv file.

This is my output:

https://imgur.com/a/XKLbmKg

This is the code for the output:

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

You can simply do as follows:

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'])

This will output a employee_file.csv which contains

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM