Reputation: 47
I'm using the praw (reddit) api to search through comments in posts for a set of words, and return that word. Basically, my word list is well... just that, a list of words:
right = [ 'i', 'he', 'she', 'it', 'we', 'have', 'has']
This is inside of words.py, which i've imported. I've saved this into a variable by iterating through it:
for word in words.right:
za = word
print za
When i print za, it prints out each individual word in words.right like I want it to. It prints:
i
he
she
it
we
have
has
My program returns the comments that contain one of these search terms just fine like so:
for comment in flat_comment_generator:
try:
if za in comment.body.lower() and comment.id not in already_done:
fob.write(comment.id + "\n")
print comment.body
print za
But when I use print za, it only prints the last term in za, not what it found in the program. For instance, it might return:
"Comment found = Yeah, I really like basketball" "Search term = has"
So everything works fine until I ask it to return that specific term.
Upvotes: 3
Views: 6789
Reputation: 162
I cannot see from your code how this search all words in comments as za will have only the last value of your word list. Yo can see all words as you print for each time you do the loop, but you won't get all if you do:
for word in words.right:
za = word
print za
I guess what you're trying to do is something like:
for comment in flat_comment_generator:
try:
if comment.id not in already_done:
terms = []
# Search all the terms
for word in words.right:
if word in comment.body.lower():
terms.append(word)
# If any term is in the comment
if len(terms) != 0:
fob.write(comment.id + "\n")
print comment.body
print terms
I hope it helps, otherwise just ask.
Upvotes: 4