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

Prasad, Ramit ramit.prasad at jpmorgan.com
Tue Oct 4 19:30:25 CEST 2011

>But I still don't know how to get the 
>statistic result of each column,

    cols = len( text[0] ) # Find out how many columns there are (assuming each row has the same number of columns)
except IndexError:
    raise #  This will make sure you can see the error while developing; 
          #  replace with whatever is appropriate for your application
results = []
for idx in xrange( cols ):
    results.append( 0 ) # Initialize an array to zero value with the correct number of columns
                        # results = [ 0, 0, 0 ]  for 3 columns
for line in text: # Check each row
    for col_idx, field in enumerate( line ): # check each column
        if token in field: 
            # Or possibly if token==field, not sure exactly what kind of comparison you need.
            results[col_idx] += 1 # token found so increment count for that column

This is a simple to understand, brute-force solution. It is not very efficient and might be slow for large amounts of data. 


Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology
712 Main Street | Houston, TX 77002
work phone: 713 - 216 - 5423

This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  

More information about the Tutor mailing list