|
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 | |
Share:
|
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
googleboy wrote: 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 | | |
googleboy <my******@yahoo.com> wrote: 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.
CSV can handle comma within the field.
--
William Park <op**********@yahoo.ca>, Toronto, Canada
ThinFlash: Linux thin-client on USB key (flash) drive http://home.eol.ca/~parkw/thinflash.html
BashDiff: Super Bash shell http://freshmeat.net/projects/bashdiff/ | | This discussion thread is closed Replies have been disabled for this discussion. Similar topics
699 posts
views
Thread by mike420@ziplip.com |
last post: by
|
3 posts
views
Thread by srijit@yahoo.com |
last post: by
|
75 posts
views
Thread by Xah Lee |
last post: by
|
5 posts
views
Thread by Michael Sperlle |
last post: by
|
5 posts
views
Thread by Cameron Laird |
last post: by
|
19 posts
views
Thread by Chris Brat |
last post: by
| | | | | | | | | | |