By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,887 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,887 IT Pros & Developers. It's quick & easy.

Question about csv writer

P: n/a
I expected the following code to work:

f = file(fn,"wb")
writer = csv.writer(f)
for i in range(IMax):
writer.writerow([dates[i]].append([ReturnHistories[j][i] 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[i]
x.append([ReturnHistories[j][i] 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 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
<tk****@hotmail.com> wrote in message
news:11**********************@i39g2000cwa.googlegr oups.com...
I expected the following code to work:

f = file(fn,"wb")
writer = csv.writer(f)
for i in range(IMax):
writer.writerow([dates[i]].append([ReturnHistories[j][i] 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[i]] (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[i].

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

-- Paul
Mar 20 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.