Reputation: 1
I am pulling data from a Rally server, and would like to write it to a CSV file and create snapshots. I am creating the file with:
orig_stdout = sys.stdout
timestr = time.strftime("%m-%d-%Y")
f = open(timestr+'FileName.csv', 'w')
sys.stdout = f
#all of the setup and functions
sys.stdout = orig_stdout
f.close()
The output I am writing to file comes from:
for item in response:
print("%s, %s, %s, %s" % (item.Attribute1, item.Attribute2, item.Attribute3, item.Attribute4))
The problem is that the strings will occasionally have a comma in it already. This is true for every usual character. I do not wish to replace the commas in said strings.
Is there a special character I can use that an Excel CSV file would recognize and be able to split into columns properly, or can I somehow tell the file to delimit using a multiple characters such as ",,"
Upvotes: 0
Views: 39
Reputation: 426
Python has a built-in csv modules that can handle various formats (such as excel) and can handle special options (such as escaping) automatically for you.
The Writer object from the CSV module sounds like what you're looking for.
Upvotes: 1