Question about csv writer

Discussion in 'Python' started by tkpmep@hotmail.com, Mar 20, 2006.

  1. Guest

    I expected the following code to work:

    f = file(fn,"wb")
    writer = csv.writer(f)
    for i in range(IMax):
    writer.writerow([dates].append([ReturnHistories[j] for j in
    range(N)]))

    but instead i got the following error message:
    Error: sequence expected

    However, if i modify the code to read
    writer = csv.writer(f)
    for i in range(IMax):
    x = dates
    x.append([ReturnHistories[j] for j in range(N)])
    writer.writerow(x)

    It works like a charm.

    Question: Why does the first form not work?

    Thomas Philips
     
    , Mar 20, 2006
    #1
    1. Advertising

  2. Paul McGuire Guest

    <> wrote in message
    news:...
    > I expected the following code to work:
    >
    > f = file(fn,"wb")
    > writer = csv.writer(f)
    > for i in range(IMax):
    > writer.writerow([dates].append([ReturnHistories[j] for j in
    > range(N)]))
    >
    > but instead i got the following error message:
    > Error: sequence expected
    >


    Probably because append returns None, not the list you just appended to.

    Also, it appears that your append statement is not doing what you want,
    appending to [dates] (that is, a temporary list containing a single
    element, the list that is the i'th element of dates), when your "working"
    example appends directly to dates.

    Sometimes (usually?) an explicit body of 2 or 3 statements is better than
    trying to cram everything into a one-liner...

    -- Paul
     
    Paul McGuire, Mar 20, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. googleboy

    Python CSV writer confusion.

    googleboy, Sep 15, 2005, in forum: Python
    Replies:
    2
    Views:
    1,786
    William Park
    Sep 16, 2005
  2. DaVinci
    Replies:
    1
    Views:
    598
    Piotr Kobzda
    Oct 5, 2006
  3. Learning Python
    Replies:
    1
    Views:
    591
    Chris Rebert
    Feb 24, 2009
  4. Replies:
    3
    Views:
    3,167
    Terry Reedy
    Apr 27, 2010
  5. Henri
    Replies:
    1
    Views:
    412
    Henri
    Jul 13, 2004
Loading...

Share This Page