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.writerow s(someiterable)
The "someiterab le" is what is confusing me.
class Image(object):
def __init__(self, title, date, genre, data, value, filename):
params = locals()
del params['self']
self.__dict__.u pdate(params)
def __repr__(self):
all_items = self.__dict__.i tems()
return '%s,%s,%s,%s,%s , %s' % (self.title, self.date,
self.genre, self.data, self.value, self.filename)
def read_images(fil ename):
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(fil ename, *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\image spipe.csv")
def get_key(*attr_n ames):
def key(image):
return [getattr(image, name) for name in attr_names]
return key
images.sort(key = get_key("filena me"))
t = open(r'D:\path\ to\filename_sor t1.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.writerow s()
#output.writero ws(image)
#output.writero w(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
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.writerow s(someiterable)
The "someiterab le" is what is confusing me.
class Image(object):
def __init__(self, title, date, genre, data, value, filename):
params = locals()
del params['self']
self.__dict__.u pdate(params)
def __repr__(self):
all_items = self.__dict__.i tems()
return '%s,%s,%s,%s,%s , %s' % (self.title, self.date,
self.genre, self.data, self.value, self.filename)
def read_images(fil ename):
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(fil ename, *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\image spipe.csv")
def get_key(*attr_n ames):
def key(image):
return [getattr(image, name) for name in attr_names]
return key
images.sort(key = get_key("filena me"))
t = open(r'D:\path\ to\filename_sor t1.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.writerow s()
#output.writero ws(image)
#output.writero w(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
Comment