[Tutor] attribute error - quick addition
Sara Johnson
sarliz73 at yahoo.com
Sun Jul 29 22:49:58 CEST 2007
Sorry...I forgot a few more lines at the end of the code. Starts with "outfile2write..."
I also added outfile2.sort()
Is this the correct traceback? Output okay except it will not sort alphabetically.
****************
Traceback (most recent call last):
File "./mymods.py", line 83, in ?
outfile2.sort()
AttributeError: 'file' object has no attribute 'sort'
*********************
Here's the code. I did not write most of this, I'm only modifying it. In fact, I'm only responsible for the last two lines.
************
#reverse the dictionary of dictionaries, making new dictonary z:
z={} #initialize empty dictionary
skeys=h[keys[0]].keys() #get the "sub-keys"
for i in skeys: #use sub-keys as new main-keys
z[i]={}
for j in keys: #use main-keys as new sub-keys
z[i][j]=h[j][i]
#use the new dictionary to find number of missing values:
print "\n fraction of sites with missing measurements:"
nsites=len(h.keys())
outfile2=open('missmeas.dat','w')
for key in skeys:
fracmiss=1.*numberMissing(z[key].values())/nsites
outstring="%s has %4.1f%% missing" % (key,100*fracmiss)
if fracmiss>0.:
print outstring
outfile2.write(outstring+'\n') #notice explicit newline \n
outfile2.sort()
outfile2.close()
----- Original Message ----
From: Alan Gauld <alan.gauld at btinternet.com>
To: tutor at python.org
Sent: Sunday, July 29, 2007 2:23:55 AM
Subject: Re: [Tutor] attribute error
"Sara Johnson" <sarliz73 at yahoo.com> wrote
> However, is there an indentation error here?
Not that I can see. Is there an error? If so can you send the
traceback?
Its very hard to answer questions without any context.
> I may just be too frustrated to see it.
>
> for key in skeys:
> fracmiss=1.*numberMissing(z[key].values())/nsites #note
> decimal multiplication, 1.*
This is a mess. Why have that long comment when all that's needed
is to add the zero?! (and some spaces to make it legible...)
fracmiss = 1.0 * numberMissing( z[key].values() ) / nsites
outstring = "%s has %4.1f%% missing" % (key, 100 * fracmiss)
if fracmiss > 0.0:
print outstring
It looks OK apart from the poor style but I don't see anything that is
an error. What are you seeing?
On the style front, if you want to force a result to a float it's more
conventional to use the float() conversion function:
fracmiss = float( numberMissing( z[key].values() ) / nsites )
And comparing to 0.0 doesn't really add anything except two
characters,
it could just as well be plain old 0.
if fracmiss > 0:
HTH,
Alan G.
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
Pinpoint customers who are looking for what you sell.
____________________________________________________________________________________
Yahoo! oneSearch: Finally, mobile search
that gives answers, not web links.
http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070729/b4625fa3/attachment.htm
More information about the Tutor
mailing list