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

Alan Gauld alan.gauld at btinternet.com
Wed Oct 5 01:46:30 CEST 2011


On 04/10/11 18:09, lina wrote:
> But I still don't know how to get the
> statistic result of each column,

Let's ignore the traversal of files issue for now.
Let's look at processing one file:

You want a list of totals of equal length to the number of columns.
We can find that number using len(line.split())

And we can initialise a list to that many elements with:

counts = [0] * numCols

Now you want to iterate over each line and for each column
increment the corresponding count if the value is equal
to your token ('E' in the example).

Lets try putting that together:

def countTokenInColumn(aFileName,token):

     lines = open(aFileName).readlines()
     numCols = len( lines[0].split() ) #use 1st line as a template
     counts = [0] * numCols

     for line in lines:
         cols = line.split()
         for index in range(numCols):
             if cols[index] == token:
                counts[index] += 1
     return counts


Now that is untested but should be close to what you want.
We could tighten it up a bit but I've tried to keep the
algorithm clear.

Does that help?

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list