Skip to content Skip to sidebar Skip to footer

Append Continuous Data To The Same Row Using Csv Lib, In Python

Here is my code: import csv with open(outputfile, 'a') as csvfile: filewrite = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL) filewrite.writerow([hostname])

Solution 1:

You're going to want to construct your row entirely before trying to write it. Try this:

import csv

withopen(outputfile, 'a') as csvfile:
    filewrite = csv.writer(csvfile, delimiter=',', quoting=csv.QUOTE_MINIMAL)
    row = [hostname]
    if (CiscoSyslog == 0):
        row.append('Syslog')
    if (CiscoSNMP == 0):
        row.append('SNMP')
    filewrite.writerow(row)

Solution 2:

The argument of cvs.writerow is a list, the list of cells. So you can build the row before outputing it, like :

row=[]
if something:
    row.append("cell1valA")
else:
    row.append("cell1valB")
row.append("cell2")
row.append("cell3")
row.append("cell4")
filewrite.writerow(row)

Post a Comment for "Append Continuous Data To The Same Row Using Csv Lib, In Python"