[Tutor] a quick Q: how to use for loop to read a series of files with .doc end

Dave Angel d at davea.name
Fri Oct 7 03:38:29 CEST 2011


On 10/06/2011 12:21 PM, lina wrote:
> <snip>
>
>>
>> As for splitting into functions, consider:
>>
>> #these two are capitalized because they're intended to be constant
>> TOKENS = "BE"
>> LINESTOSKIP = 43
>> INFILEEXT = ".xpm"
>> OUTFILEEXT = ".txt"
>>
>> def dofiles(topdirectory):
>>     for filename in os.listdr(topdirectory):
>>         processfile(filename)
>>
>> def processfile(infilename):
>>     base, ext =os.path.splitext(fileName)
>>     if ext == INFILEEXT:
>>         text = fetchonefiledata(infilename)
>>         numcolumns = len(text[0])
>>         results = {}
>>         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
>>
> I still have trouble understanding the results[ch][col] part.
>
> Thanks ahead,
>
>
First ask yourself what results consists of.  It's a dictionary, which 
is a aggregate of pairs of key/value items.  In each case the key is a 
character, and the the value is a list of numbers.

So results[ch] is a particular value, in other words a list.

And results[ch] [col] is a particular item of the list, in other words, 
and integer

If we use += 1 on that integer, we increment it by 1

Is that clear?   if not, write a separate program to build such as 
structure, and try printing out each level of item
      print results["E"]
should display a list
       print results["E"][0]
should display an integer
       print results["E"][1]
       print results["E"][2]


Does this help?

-- 

DaveA



More information about the Tutor mailing list