[Tutor] a quick Q: how to use for loop to read a series of files with .doc end
lina
lina.lastname at gmail.com
Fri Oct 7 15:58:36 CEST 2011
On Fri, Oct 7, 2011 at 9:08 PM, Andreas Perstinger <
andreas.perstinger at 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 at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor<http://mail.python.org/mailman/listinfo/tutor>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111007/7336db79/attachment.html>
More information about the Tutor
mailing list