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

lina lina.lastname at gmail.com
Wed Oct 5 04:22:05 CEST 2011


On Wed, Oct 5, 2011 at 1:30 AM, Prasad, Ramit <ramit.prasad at jpmorgan.com>wrote:

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

>
> try:
>    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;
>
This part:

It's showed up:

    except IndexError:
         ^
SyntaxError: invalid syntax

for fileName in os.listdir("."):
    if os.path.isfile(fileName) and os.path.splitext(fileName)[1]==".xpm":
        filedata = open(fileName)
        text=filedata.readlines()
        cols = len(text[0])
        except IndexError:
            print ("Index Error.")
        result=[]
        for idx in xrange(cols):
            results.append(0)
        for line in text:
            for col_idx, field in enumerate(line):
                if token in field:
                    results[col_idx]+=1
            for index in col_idx:
                print results[index]

it showed up:

    print results[]
                ^
SyntaxError: invalid syntax

Sorry, I am still lack deep understanding about something basic. Thanks for
your patience.



>          #  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
>
>
> 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.
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Best Regards,

lina
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20111005/2127af67/attachment-0001.html>


More information about the Tutor mailing list