When counting the appearance of a line in a file, my code does not take into account the very first word

code

def main():
try:
    file=input('Enter the name of the file you wish to open: ')
    thefile=open(file,'r')
    line=thefile.readline()
    line=line.replace('.','')
    line=line.replace(',','')
    thefilelist=line.split()
    thefilelistset=set(thefilelist)
    d={}
    for item in thefilelist:
        thefile.seek(0)
        wordcount=line.count(' '+item+' ')
        d[item]=wordcount
    for i in d.items():
        print(i)   
    thefile.close()
except IOError:
    print('IOError: Sorry but i had an issue opening the file that you specified to READ from please try again but keep in mind to check your spelling of the file you want to open')
main()

Problem

Basically, I try to read the file and count the number of times each word in the file appears, and then print that word with the number of times that it appeared next to it.

Everything works, except that it will not read the first word in the file.

File i am using

my practice file in which I am testing this code contains this text:

This file is intended for testing. He will check how many times the words appear here.

Exit

('for', 1)
('going', 1)
('the', 1)
('testing', 1)
('is', 2)
('file', 1)
('test', 1)
('It', 1)
('This', 0)
('appear', 1)
('to', 1)
('times', 1)
('here', 1)
('how', 1)
('in', 1)
('words', 1)
('many', 1)

Note

If you notice that this means that 'This' appears 0 times, but it does appear in the file.

any ideas?

+4
6

, :

wordcount=line.count(' '+item+' ')

"This" .

, , with .readlines()

pythons. . , . , words... lastwordofsentence.Firstwordofnextsentence, , . , , '' ' ', split .

, Python 2.7 3.X. .

filename = input('Enter the name of the file you wish to open: ')
# Using a with block like this is cleaner and nicer than try catch
with open(filename, "r") as f:
    all_lines = f.readlines()

d={} # Create empty dictionary

# Iterate through all lines in file
for line in all_lines:

    # Replace periods and commas with spaces
    line=line.replace('.',' ')
    line=line.replace(',',' ')

    # Get all words on this line
    words_in_this_line = line.split() # Split into all words

    # Iterate through all words
    for word in words_in_this_line:
        #Check if word already exists in dictionary
        if word in d: # Word exists increment count
            d[word] += 1
        else: #Word doesn't exist, add it with count 1
            d[word] = 1

# Print all words with frequency of occurrence in file
for i in d.items():
    print(i)  
+3

:

wordcount=line.count(' '+item+' ')

"" + "" + "", .

+7

Python. , .

, , ; : , .

Python " " (https://docs.python.org/2/library/collections.html#collections.Counter), .

; "re.split()" (https://docs.python.org/2/library/re.html#regular-expression-syntax).

, split() wordcounter . , re.sub() split.

import re, collections

with open(raw_input('Enter the name of the file you wish to open: '), 'r') as file:
    for d in reduce(lambda acc, line: acc.update(re.split("\W", line)) or acc,
                     map(lambda line: re.sub("(\.,)", "", line), file),
                     collections.Counter()).items():
        print d
+4

, line ' '+item+' ', , . "This" , .

, :

wordcount=(' '+line+' ').count(' '+item+' ')

, .

+1

wordcount=line.count(' '+item+' '). . :

import string

def main():
    try:
        #file=input('Enter the name of the file you wish to open: ')
        thefile=open('C:/Projects/Python/data.txt','r')
        line=thefile.readline()
        line = line.translate(string.maketrans("",""), string.punctuation)
        thefilelist=line.split()
        d={}
        for item in thefilelist:
            if item not in d:
                d[item] = 0
            d[item] = d[item]+1 
        for i in d.items():
            print(i)   
        thefile.close()
    except IOError:
        print('IOError: Sorry but i had an issue opening the file that you specified to READ from please try again but keep in mind to check your spelling of the file you want to open')


main()
+1

This ' '.

:

line= ' ' + thefile.readline()

. :

  • ?
  • . ?
0

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


All Articles