Reputation: 33
Hi I'm trying to print write on a .csv file on python and I want my output to only be separated by commas. I removed the brackets from print() so that no parenthesis were present but this also cleared out commas. How can I fix this? This is my code:
print self.t, len(self.sAgentList), len(self.iAgentList), len(self.rAgentList)
Upvotes: 0
Views: 99
Reputation: 309861
The best solution is to use a csv.writer
to write the data.
import csv
import sys
writer = csv.writer(sys.stdout)
writer.writerow(['1', '2', '3'])
Here I write to sys.stdout
, but you can write to whatever file-like object that you like. Note that writerow
takes a list of strings. If that isn't what you have, you need to make sure that the inputs are strings... e.g.:
writer.writerow([
str(item) for item in
(self.t, len(self.sAgentList), len(self.iAgentList), len(self.rAgentList))])
If you want to use print
, you can supply the sep
argument (assuming that you are using the print function rather than python2.x's print statement):
from __future__ import print_function
print(1, 2, 3, sep=',')
Upvotes: 1