Attempting to count words in a file using Python

I am trying to count the number of “hard words” in a file, which requires me to count the number of letters in each word. For now, I'm only trying to get individual words, one at a time, from a file. I wrote the following:

file = open('infile.txt', 'r+')
fileinput = file.read()

for line in fileinput:
    for word in line.split():
        print(word)

Output:

t
h
e

o
r
i
g
i
n

.
.
.

It seems to print one character at a time, and not one word at a time. I would really like to know more about what is really happening here. Any suggestions?

+4
source share
3 answers

Use splitlines () :

fopen = open('infile.txt', 'r+')
fileinput = fopen.read()

for line in fileinput.splitlines():
    for word in line.split():
        print(word)

fopen.close()

Without splitlines () :

, . :

with open('infile.txt', 'r+') as fopen:
    for line in fopen:
        for word in line.split():
            print(word)
+6

, ,

with open('infile.txt', 'r+') as f:
    for line in f:
        for word in line.split():
            print(word)

, , -

def is_difficult(word):
    return len(word)>5

with open('infile.txt', 'r+') as f:
    words = (w for line in f for w in line.split() if is_difficult(w))
    for w in words:
        print(w)

ciao come va
oggi meglio di domani
ieri peggio di oggi

meglio
domani
peggio
+3

, .read(), , , for line in fileinput char char, -, , readlines.

, dict, , , str.strip:

def words(n, fle):
    from collections import defaultdict
    d = defaultdict(list)
    from string import punctuation
    with open(fle) as f:
        for line in f:
            for word in line.split():
                word = word.strip(punctuation)
                _len = len(word)
                if _len >= n:
                    d[_len].append(word)
    return d

dict , , n.

0
source

Source: https://habr.com/ru/post/1614497/


All Articles