NinaDev NinaDev - 3 years ago 199
Python Question

the print function deos not work in python

I have been trying to find some keywords listed a file in another file, if the keyword is found, then print the keyword. Here is my code, python does not generate any errors but I do not see anything printed either. (I am pretty sure that keywords are existed in the file that I used the find function on)

keyword_array = []
with open('localDrive\\C0577785-MeshID.txt') as my_keywordfile:
for keyword in my_keywordfile.readlines():
keyword_array.append(keyword.lower())
# print(keyword)

with open('localdrive\\file') as my_dataset:
for line in my_dataset.readlines():
for keywords in keyword_array:
if line.lower().find(keywords) >= 0:
print("keywords:", keywords,"\n")
break


Am I missing anything?
Thanks

Answer Source

When u read a line from a file, it has a '\n'(linux) or '\r\n'(windows) in the end of string.

So before append to the list, u should strip the '\n' use strip() func. Hope that can help you

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download