I'm writing python scripts to count how many times certain strings occur but it seems like the else is not working correctly.
Here is my codes:
import os
user_input = "#"
directory = os.listdir(user_input)
searchstring1 = 'type="entity"'
searchstring2 = 'type="field"'
searchstring3 = 'type="other"'
searchstring4 = "type="
count_entity = 0
count_field = 0
count_other = 0
count_none = 0
counttotal = 0
for fname in directory:
if os.path.isfile(user_input + os.sep + fname):
f = open(user_input + os.sep + fname, 'r', encoding="utf-8")
for line in f:
if "<noun" in line:
counttotal += 1
if searchstring1 in line:
count_entity += 1
if searchstring2 in line:
count_field += 1
if searchstring3 in line:
count_other += 1
else:
count_none += 1
f.close()
print("Entity Number" + str(count_entity))
print("Field Number" + str(count_field))
print("Other Number" + str(count_other))
print("None Number" + str(count_none))
If it worked correctly, the count_none should equal to total-entity-field-other. But I don't know why it turns out count_none = counttotal so obvious else is not working correctly.
Can anyone tell me why this happened? Thanks for your help!!