1

I am trying to read the words in a File looks its not working as expected. Can you please let me know if I am missing anything.

from collections import Counter

wordcount=Counter(f1.read())

for k,v in wordcount.items():
    print (k ,v)

File contents are:

DELETE
INSERT
DELETE
INSERT
UPDATE
UPDATE

Expecting

DELETE  2
INSERT 2 ..

..

but it is counting the letters

Gambit1614
  • 8,547
  • 1
  • 25
  • 51
user826407
  • 159
  • 1
  • 8

4 Answers4

1

use .readlines()

.read() return char continuously. so Counter counts the char. but .readlines() return word(The fact is a line, but in your case, a word in a line)

Jiun Bae
  • 550
  • 3
  • 14
1

Use readlines() instead of read,

from collections import Counter

f1 = open("test.txt", "r")
wordcount=Counter(f1.readlines())

#print(wordcount)

for k,v in wordcount.items():
    print (k ,v)

To get better result use split() or splitlines() to remove \n

wordcount=Counter(f1.readlines().splitlines())
# or
wordcount=Counter(f1.read().split())

Output:

DELETE 2
INSERT 2
UPDATE 2
bhansa
  • 7,282
  • 3
  • 30
  • 55
1

You have to use readlines() instead of read(). Also you need to get rid of \n characters as well since using readlines() will read them as well.

from collections import Counter

with open('chk.txt') as f:
    mylist = f.read().splitlines()   #get rid of newline character

wordcount=Counter(mylist)

for k,v in wordcount.items():
    print (k ,v)

#Output:
('INSERT', 2)
('UPDATE', 2)
('DELETE', 2)
Gambit1614
  • 8,547
  • 1
  • 25
  • 51
1

Just change your argument for Counter. From

wordcount=Counter(f1.read())

To

wordcount=Counter(f1.readlines().split())