473,325 Members | 2,671 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

Question about csv writer

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
1 2397
<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Hans De Winter | last post by:
Hi, I am really puzzled why my compiler does not accept the following code:- --- CODE STARTS HERE --- #include <iostream> using namespace std; class Foo {
4
by: Matt Osborne | last post by:
Hello all. I have an application that i am writing to automate an older system. The system is based on a 16-bit DOS application. It apears to me that I am unable to redirect input and output...
1
by: Chris Fink | last post by:
I have a general question regarding this piece of code that I wrote. This works fine, however I am confused as to how I am able to pull the sb instance out at the end? When I create the...
6
by: Nikolay Petrov | last post by:
I need to transfer different types of data from my tcp client to my tcp server - string, bynary, datasets. Ho to know what exactly data I receive?
16
by: akantrowitz | last post by:
In csharp, what is the correct locking around reading and writing into a hashtable. Note that the reader is not looping through the keys, simply reading an item out with a specific key: If i...
10
by: Gerard Flanagan | last post by:
Hello I have the following code: #### builder.py ######### class HtmlBuilder(object): @staticmethod def page(title=''): return HtmlPage(title)
2
by: Harry | last post by:
Hi, I am using XMLWriter to build xml and I need to build the element below. How do I do this? <Password format="encrypted">password</Password> Thanks
4
by: alacrite | last post by:
I have a class that I want to turn its contents into csv file. I want to be able to set the value of the delimiter, the name of the file it gets saved to, the path of that file, and maybe a few...
1
by: David | last post by:
I have the following two button click procedures to retrieve html code from web sites. The code for Button1 seems to work fine. Button2 works fine for http://yahoo.com but not for...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.