简体   繁体   中英

How do I make the “for” loop cycle through all lines of a text file in python 3.8?

I have a text file "abc.txt" containing several lines of text. I am trying to count and list the frequency of appearance of each letter of the alphabet, a till z, in the txt file.

In the code below, after checking for the letter 'a' correctly, it returns 0 as count for the rest of the alphabets from 'b' onwards. Once the 'for' loop is executed for the letter 'a', for the subsequent letters, how I make it loop back to the beginning of the text again?

with open(r"C:\Users\username\Downloads\abc.txt","r") as x:
    for j in "abcdefghijklmnopqrstuvwxyz":
        n = 0
        for i in x:
            y = i.count(j)
            n += y
        print(n)

This is because once you've read the entire file, the "pointer" or "cursor" is now at the end of the file, there are no more lines to read, and thus the rest of the checks end with 0.

To fix this move the cursor back to the top of the file after each loop of the outer for loop using the seek() function:

import os

with open(r"C:\Users\username\Downloads\abc.txt","r") as x:
    for j in "abcdefghijklmnopqrstuvwxyz":
        n = 0
        for i in x:
            y = i.count(j)
            n += y
        print(n)
        x.seek(0, os.SEEK_SET)

EDIT:

It is probably worth making everyline lowercase before counting:

y = i.lower().count(j)

Your file handle x is an exhausted iterator after the iteration. You would have to reset it to the beginning of the file after each counting loop. But it is way more efficient to count all letters in one iteration:

from collections import Counter

cnt = Counter()
with open(r"C:\Users\username\Downloads\abc.txt","r") as x:
    for i in x:
        cnt.update(i)
for j in "abcdefghijklmnopqrstuvwxyz":
    print(cnt[j])

Try:

count = {}
with open(r"C:\Users\username\Downloads\abc.txt","r") as x:
    srt = x.read()
    for i in srt:
        if i not in count:
            count[i] = srt.count(i)
            srt.replace(i,'')

After iterating over the file the first time with the for i in x loop, the file is exhausted. All the other for i in x loops will not run at all.

Instead of iterating over the file directly, load the file contents into a variable which can be examined repeatedly.

Also, it looks like you don't actually need to scan the file line-by-line; you can examine the entire file contents in one step.

Try this code:

with open(r"C:\Users\username\Downloads\abc.txt","r") as x:
    text = x.read()

for letter in "abcdefghijklmnopqrstuvwxyz":
    frequency = text.count(letter)
    print(frequency)

You can use Counter for this.

from collections import Counter
from string import ascii_lowercase


c = Counter()
letters = set(ascii_lowercase)
with open(r"C:\Users\username\Downloads\abc.txt","r") as f:
    c.update(letter for line in f for letter in line if letter in letters)

You don't need to scan the entire file for each letter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM