[Tutor] character counter

Richard Lovely roadierich at googlemail.com
Sun Jun 28 01:09:34 CEST 2009


2009/6/27 julie <youthcares.iiba at gmail.com>:
> Hello,
>
> I need help with the following problem:
>
> Write a loop that reads each line of a file and counts the number of lines
> that are read until the total length of the lines is 1,000 characters. Use a
> break statement to make sure that you don't continue reading the file once
> the 1,000 characters are read.
>
> I figured out how to open a file, count and print the lines, however I
> cannot figure out or find online ...or anywhere else how to count characters
> in a file. This is what i have so far:
>
> file = open("/Users/meitalamitai/Documents/Computer
> Science/Python/Homework/Lorem_Ipsum.py")
> lines = 0
> for line in file:
>     lines=lines+1
> print '%r has %r lines' % ("Lorem_Ipsum.py", lines)
>    if char >= 1000:
>         break
>
> Thanks!
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
In python, we don't explictily count, unless we absolutely have to.
Instead, in your situation, we measure the length of things.  Length
is what you need to be searching for.  Specifically, finding the
length of a string.

-- 
Richard "Roadie Rich" Lovely, part of the JNP|UK Famile
www.theJNP.com


More information about the Tutor mailing list