On Fri, Oct 7, 2011 at 9:08 PM, Andreas Perstinger < andreas.perstin...@gmx.net> wrote:
> On 2011-10-07 14:21, lina wrote: > >> I don't know why this gives a key error on 'E' (which basically means >>>> that >>>> there is no key 'E') since the code above should guarantee that it >>>> exists. >>>> Odd. I'm also not sure why the error occurs after it prints summary. >>>> Are you >>>> sure the output is in the sequence you showed in your message? >>>> >>>> One simple explanation: it continued on to the next file, which has >>>> >>> neither "E" nor "B" in it. >>> >>> In this directory, I only kept one file. try.xpm >> > > That's wrong. > > > $ more try.xpm >> aaEbb >> aEEbb >> EaEbb >> EaEbE >> >> $ ls >> counter-vertically-v2.py try.xpm >> counter-vertically.py try.txt >> > > As "ls" proves, you have *4* files in the directory. > > You start your script with "dofiles(".")". This function gets a list of > *all* files in the directory in an *arbitrary* order and processes each of > it. > > In your function "processfile" you first create an empty dictionary > ("results = {}") and then you put values into the dictionary *only* for > xpm-files ("if ext == INFILEEXT:"). *But* you print the summary for *every* > file because the indentation at the end of the function is outside the > if-branch where you check for the file extension. So for every file which > isn't a xpm-file, "results" is an empty dictionary (see first line of the > function) and therefore "zip(results['E'], results['B'])" will throw an > exception. > > How to solve it? I personally would check for the file extension in the > function "dofiles" and would only continue with xpm-files (in other words > move the if-statement from "processfile" to "dofiles".) > def processfile(infilename): results={} base, ext =os.path.splitext(infilename) if ext == INFILEEXT: text = fetchonefiledata(infilename) numcolumns=len(text[0]) for ch in TOKENS: results[ch] = [0]*numcolumns for line in text: line = line.strip() for col, ch in enumerate(line): if ch in TOKENS: results[ch][col]+=1 for k,v in results.items(): print(results) summary=[] for a,b in zip(results['E'],results['B']): summary.append(a+b) writeonefiledata(base+OUTFILEEXT,summary) else: os.sys.exit() This part has already given the indentation. if moved it a bit further, it would show more like: $ python3 counter-vertically-v2.py {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]} {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]} {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]} {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 0, 0, 0, 0]} {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]} {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]} {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]} {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 0, 0]} {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]} {'B': [0, 0, 0, 0, 0, 0], 'E': [1, 0, 1, 0, 1, 0]} Thanks again, > > Bye, Andreas > > > ______________________________**_________________ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor> >
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor