473,546 Members | 2,243 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2406
<tk****@hotmail .com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.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
2001
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
8865
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 to and from the application. Does anybody know if i am missing something simple? Thanks
1
1177
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 XmlTextWriter class and pass it in sw (which in turn sw was created with sb) how is the sb object being maintained and populated via the writer instance? My...
6
1426
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
3438
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 have the following hashtable h which has multiple readers and 1 writer (on different threads) is this the correct locking below: lock (h.syncroot)...
10
1985
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
2274
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
2040
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 other things. What would be a good design to accomplish these goals? Here are the ideas that I have come up with: Class X = class with data that I...
1
1115
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 http://groups.google.com. When trying the Google site, iContentLength is -1 after it is set to myResponse.ContentLength. Any ideas why? If I uncomment the...
0
7507
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7698
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7794
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5361
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3492
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
1922
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.