Reputation: 85
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
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