Question about csv writer

T

tkpmep

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
 
P

Paul McGuire

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top