Danny
Danny

Reputation: 85

Writing number of occurrences to a file

I am counting the number of occurrences of words in a list (called a_master). The words to search for and count are in dictionary.txt. The problem is, when I write the count to file, it comes out like this:

1Count cloud
19Count openstack
3 

And here is the code:

with open("dictionary.txt","r") as f:
for line in f:
    if a_master.count(line.strip()) !=0:
        file.write( "Count " + line + str((a_master).count(line.strip())))

As you can see, for some reason when it's outputting the number, it puts it on a new line and i have no idea why!

Upvotes: 1

Views: 68

Answers (1)

user1846747
user1846747

Reputation:

Use .strip() on line.

Try this

with open("dictionary.txt","r") as f:
for line in f:
    if a_master.count(line.strip()) !=0:
        file.write( "Count " + line.strip() + str((a_master).count(line.strip())))

Upvotes: 1

Related Questions