PDA

View Full Version : Python: CSV module


Norb
01-10-2008, 09:19 PM
Has anyone encountered a way to get this to work properly? My 'data' will be a user created list, so I can't hardcode it.

I do NOT want this:

import csv
import sys

data = ["one", "two"]
writer = csv.writer(sys.stdout) #prints to debug window instead of a file
writer.writerows(data)


The above code returns:

o,n,e
t,w,o


What I want it to do is return this:


one
two

Chadrik
01-10-2008, 11:58 PM
a single row is a sequence of strings or numbers. the writer method expects a sequence of rows. so i think this is what you want:


import csv
import sys

data = [["one"], ["two"]]
writer = csv.writer(sys.stdout) #prints to debug window instead of a file
writer.writerows(data)

Norb
01-11-2008, 09:09 PM
It was an issue inputting a user selected list in there (for me anyways..) so I ended up doing this:


def writelist_aslines(self, list):
"""Writes each element of the list to a separate line"""
fid = open(self.filename, 'a')
for l in list:
fid.write(str(l)+'\n')
fid.close()

CGTalk Moderation
01-11-2008, 09:09 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.