Python CSV writer confusion.

G

googleboy

Hi. I am trying to write out a csv file with | instead of comma,
because I have a field that may have many commas in it. I read in a
csv file, sort it, and want to write it out again.

I read the example that says:

import csv
writer = csv.writer(open("some.csv", "wb"))
writer.writerows(someiterable)

The "someiterable" is what is confusing me.



class Image(object):
def __init__(self, title, date, genre, data, value, filename):
params = locals()
del params['self']
self.__dict__.update(params)
def __repr__(self):
all_items = self.__dict__.items()
return '%s,%s,%s,%s,%s, %s' % (self.title, self.date,
self.genre, self.data, self.value, self.filename)

def read_images(filename):
csv_file = open(filename, "rb")
reader = csv.reader(csv_file, dialect='excel', delimiter='|')
images = [Image(*[field.strip() for field in row]) for row in
reader]
csv_file.close()
return books

def sort_images(filename, *attr_names):
csv_file = open(filename, "rb")
reader = csv.reader(csv_file, dialect='excel', delimiter='|')


if __name__ == '__main__':
images = read_images(r"D:\path\to\imagespipe.csv")

def get_key(*attr_names):
def key(image):
return [getattr(image, name) for name in attr_names]
return key

images.sort(key = get_key("filename"))

t = open(r'D:\path\to\filename_sort1.csv', 'w')

for image in images:
print book
#t.write('%s\n' % book) %Before I needed | delimited, this
worked
#csv.writer(t, dialect='excel', delimiter='|')
output = csv.writer(t, dialect='excel', delimiter='|')
output.writerows()
#output.writerows(image)
#output.writerow(image)

t.close()



This returns an error that says "Error: sequence expected"

My understanding of this is that I am creating a list of lists and I am
iterating over it (for image in images), and that image is a list, and
is therefore iterable...?

I am a bit new at this, and would greatly appreciate any assistance.

TIA

googleboy
 
L

Larry Bates

The someiterable should be something that has a .next
method. That would be a list or any object with such a
method. In your case it would be the images list.
The writer method will iterate over the list and write
everything out for you. Don't put it inside a loop
yourself. More like (not tested):

import csv
..
..
..
outfile=open(r'D:\path\to\filename_sort1.csv', 'w')
CSVwriter=csv.writer(outfile, dialect='excel', delimiter='|')
CSVwriter.writerows(images)


Larry Bates
if __name__ == '__main__':
images = read_images(r"D:\path\to\imagespipe.csv")

def get_key(*attr_names):
def key(image):
return [getattr(image, name) for name in attr_names]
return key

images.sort(key = get_key("filename"))

t = open(r'D:\path\to\filename_sort1.csv', 'w')

for image in images:
print book
#t.write('%s\n' % book) %Before I needed | delimited, this
worked
#csv.writer(t, dialect='excel', delimiter='|')
output = csv.writer(t, dialect='excel', delimiter='|')
output.writerows()
#output.writerows(image)
#output.writerow(image)

t.close()


-Larry Bates

Hi. I am trying to write out a csv file with | instead of comma,
because I have a field that may have many commas in it. I read in a
csv file, sort it, and want to write it out again.

I read the example that says:

import csv
writer = csv.writer(open("some.csv", "wb"))
writer.writerows(someiterable)

The "someiterable" is what is confusing me.



class Image(object):
def __init__(self, title, date, genre, data, value, filename):
params = locals()
del params['self']
self.__dict__.update(params)
def __repr__(self):
all_items = self.__dict__.items()
return '%s,%s,%s,%s,%s, %s' % (self.title, self.date,
self.genre, self.data, self.value, self.filename)

def read_images(filename):
csv_file = open(filename, "rb")
reader = csv.reader(csv_file, dialect='excel', delimiter='|')
images = [Image(*[field.strip() for field in row]) for row in
reader]
csv_file.close()
return books

def sort_images(filename, *attr_names):
csv_file = open(filename, "rb")
reader = csv.reader(csv_file, dialect='excel', delimiter='|')


if __name__ == '__main__':
images = read_images(r"D:\path\to\imagespipe.csv")

def get_key(*attr_names):
def key(image):
return [getattr(image, name) for name in attr_names]
return key

images.sort(key = get_key("filename"))

t = open(r'D:\path\to\filename_sort1.csv', 'w')

for image in images:
print book
#t.write('%s\n' % book) %Before I needed | delimited, this
worked
#csv.writer(t, dialect='excel', delimiter='|')
output = csv.writer(t, dialect='excel', delimiter='|')
output.writerows()
#output.writerows(image)
#output.writerow(image)

t.close()



This returns an error that says "Error: sequence expected"

My understanding of this is that I am creating a list of lists and I am
iterating over it (for image in images), and that image is a list, and
is therefore iterable...?

I am a bit new at this, and would greatly appreciate any assistance.

TIA

googleboy
 
W

William Park

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top