<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman, new york, times, serif;font-size:12pt"><DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">Sorry...I forgot a few more lines at the end of the code. Starts with "outfile2write..."</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">I also added outfile2.sort() <BR><BR></DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"><BR>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">Is this the correct traceback? Output okay except it will not sort alphabetically.</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">****************</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">Traceback (most recent call last):<BR> File "./mymods.py", line 83, in ?<BR> outfile2.sort()<BR>AttributeError: 'file' object has no attribute 'sort'<BR>*********************</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">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.</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> </DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">************</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">#reverse the dictionary of dictionaries, making new dictonary z:<BR>z={} #initialize empty dictionary<BR>skeys=h[keys[0]].keys() #get the "sub-keys"<BR>for i in skeys: #use sub-keys as new main-keys<BR> z[i]={}<BR> for j in keys: #use main-keys as new sub-keys<BR> z[i][j]=h[j][i]<BR>#use the new dictionary to find number of missing values:<BR>print "\n fraction of sites with missing measurements:"<BR>nsites=len(h.keys())<BR>outfile2=open('missmeas.dat','w')<BR>for key in skeys:<BR> fracmiss=1.*numberMissing(z[key].values())/nsites<BR> outstring="%s has %4.1f%% missing" % (key,100*fracmiss)</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"> if fracmiss>0.:<BR> print outstring</DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"><BR> outfile2.write(outstring+'\n') #notice explicit newline \n<BR>outfile2.sort()<BR>outfile2.close()<BR><BR> </DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif">----- Original Message ----<BR>From: Alan Gauld <alan.gauld@btinternet.com><BR>To: tutor@python.org<BR>Sent: Sunday, July 29, 2007 2:23:55 AM<BR>Subject: Re: [Tutor] attribute error<BR><BR>
<DIV>"Sara Johnson" <sarliz73@yahoo.com> wrote<BR><BR>> However, is there an indentation error here?<BR><BR>Not that I can see. Is there an error? If so can you send the <BR>traceback?<BR>Its very hard to answer questions without any context.<BR><BR>> I may just be too frustrated to see it.<BR>><BR>> for key in skeys:<BR>> fracmiss=1.*numberMissing(z[key].values())/nsites #note <BR>> decimal multiplication, 1.*<BR><BR>This is a mess. Why have that long comment when all that's needed<BR>is to add the zero?! (and some spaces to make it legible...)<BR><BR> fracmiss = 1.0 * numberMissing( z[key].values() ) / nsites<BR> outstring = "%s has %4.1f%% missing" % (key, 100 * fracmiss)<BR> if fracmiss >
0.0:<BR> print outstring<BR><BR>It looks OK apart from the poor style but I don't see anything that is<BR>an error. What are you seeing?<BR><BR>On the style front, if you want to force a result to a float it's more<BR>conventional to use the float() conversion function:<BR><BR> fracmiss = float( numberMissing( z[key].values() ) / nsites )<BR><BR>And comparing to 0.0 doesn't really add anything except two <BR>characters,<BR>it could just as well be plain old 0.<BR><BR> if fracmiss > 0:<BR><BR>HTH,<BR><BR>Alan G.<BR><BR><BR><BR>_______________________________________________<BR>Tutor maillist - Tutor@python.org<BR><A href="http://mail.python.org/mailman/listinfo/tutor" target=_blank
rel=nofollow>http://mail.python.org/mailman/listinfo/tutor</A></DIV></DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"><BR></DIV></DIV><BR>
<HR SIZE=1>
<A href="http://us.rd.yahoo.com/evt=48250/*http://searchmarketing.yahoo.com/arp/sponsoredsearch_v9.php?o=US2226&cmp=Yahoo&ctv=AprNI&s=Y&s2=EM&b=50" target=_blank rel=nofollow>Pinpoint customers </A>who are looking for what you sell. </DIV>
<DIV style="FONT-SIZE: 12pt; FONT-FAMILY: times new roman, new york, times, serif"><BR></DIV></div><br>
<hr size=1>Building a website is a piece of cake. <br>Yahoo! Small Business gives you <a href="http://us.rd.yahoo.com/evt=48251/*http://smallbusiness.yahoo.com/webhosting/?p=PASSPORTPLUS">all the tools to get online.</a></body></html>