-2

I am currently trying to count the characters per line and the first 1,000 characters of a txt file. My code counts the characters of the file and produces no error, but does not stop the count at 1,000. I know there needs to be a break to fix this problem, but I do not know what I'm doing wrong. This is my first post and would like to apologize in advance if I'm not being succinct or clear enough.

This keeps the values of characters at over 1,000 but does not print them:

with open('myfile', 'r') as f:
    characters = 0
    for lines in f.readlines():
        length = len(lines) - lines.count('\n')
        characters += sum(len(length) for length in lines)
        if characters >= 1000:
            break
    print('the number of characters in this line is: %s' % length)
    print('the total number of characters is: %s' % characters)

and this also keeps the values of characters at over 1,000 and prints them:

with open('myfile', 'r') as f:
    characters = 0
    for lines in f.readlines():
        length = len(lines) - lines.count('\n')
        characters += sum(len(length) for length in lines)
        print('the number of characters in this line is: %s' % length)
        print('the total number of characters is: %s' % characters)
JCPowell
  • 21
  • 2
  • What is `characters += sum(len(length) for length in lines)` supposed to be doing in addition to `length = len(lines) - lines.count('\n')`? – MattDMo Mar 31 '22 at 23:20
  • @MattDMo I was using `length = len(lines) - lines.count('\n')` to count the number of characters per line and `characters += sum(len(length) for length in lines)` to count total characters in the file. – JCPowell Mar 31 '22 at 23:24
  • If you want to iterate line-by-line but you only want to consider the first 1000 characters in the file, maybe consider iterating over something like `lines = f.read()[:1000].split("\n")`, rather than iterating over `f.readlines()` directly? – Oliver Tonnesen Mar 31 '22 at 23:25
  • I think really the crux of your issue is that you're acting on whole lines but you're trying to reason about individual characters. – Oliver Tonnesen Mar 31 '22 at 23:27
  • 1
    You're probably adding to your confusion by using a poor variable name. `lines` contains just one line in each iteration of the `for` loop. – Barmar Mar 31 '22 at 23:42
  • `sum(len(length) for length in lines)` is the same as `len(lines)` since `length` is a single character and `len(length)` is always `1`. – Barmar Mar 31 '22 at 23:44
  • @Barmar thanks I made the change you suggested regarding my variable name. Also, as Oliver pointed out, changing to `readline()` or `read()` allows the count to stop at 1,000 characters using either the code he suggested or an `if` statement; however, now it interprets each individual character as a line instead of the character length of the individual line. – JCPowell Mar 31 '22 at 23:54
  • That sounds like you ignored `.split("\n")` at the end of Olivier's suggested code. – Barmar Mar 31 '22 at 23:56

1 Answers1

-1
   with open('myfile', 'r') as f:
   ch = 0
   for line in f.readlines():
       length = len(line)
       ch = ch + len(line)
       print('the number of characters in this line is: %d' % length)
       if ch >= 1000:
           break
   f.close()
JCPowell
  • 21
  • 2