Python CSV writer confusion.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • googleboy

    #1

    Python CSV writer confusion.

    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

  • Larry Bates

    #2
    Re: Python CSV writer confusion.

    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\file name_sort1.csv' , 'w')
    CSVwriter=csv.w riter(outfile, dialect='excel' , delimiter='|')
    CSVwriter.write rows(images)


    Larry Bates[color=blue]
    >
    >
    > 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()[/color]


    -Larry Bates


    googleboy wrote:[color=blue]
    > 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
    >[/color]

    Comment

    • William Park

      #3
      Re: Python CSV writer confusion.

      googleboy <mynews44@yahoo .com> wrote:[color=blue]
      > 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.[/color]

      CSV can handle comma within the field.

      --
      William Park <opengeometry@y ahoo.ca>, Toronto, Canada
      ThinFlash: Linux thin-client on USB key (flash) drive

      BashDiff: Super Bash shell
      Compare the best free open source Software Development Software at SourceForge. Free, secure and fast Software Development Software downloads from the largest Open Source applications and software directory

      Comment

      Working...