-6

I am total beginner so keep this as simple, as possible. I am attempting to make this particular code to work but I am not understanding, what is really happening in this if any(post.title... loop.

import feedparser
d = feedparser.parse('feed0.rss', 'r')
with open("./mystuff.txt", 'r') as mystuff:
    mylines = mystuff.readlines()
    for post in d.entries:
        for myline in mylines:
   --->     if any(post.title in myline for myline in mylines):
                print( "Here is one: " + post.title + " " + post.link )

My attempts to get an answer from Reddit (/r/learnpython) has failed and hopefully someone here can help me to understand this.

Second problem is that this code prints out the "Here is one: " + post.title + as many times as there is lines in mystuff.txt.

halfer
  • 19,824
  • 17
  • 99
  • 186
J.D Joe
  • 1
  • 5
  • 3
    Look at how many times `for myline in mylines` appears in that block. – TigerhawkT3 Apr 07 '16 at 07:47
  • 1
    With `for loop in mylines` it loops through every entry in `mylines` and checks if you have the `post.title in myline` for `any` of those entries. But why do you put this in another loop over `mylines? – Matthias Apr 07 '16 at 08:37

2 Answers2

1

Please try to make your code a bit readable. Too many for and you can still stretch the below code.

import feedparser
d = feedparser.parse('feed0.rss', 'r')
with open("./mystuff.txt", 'r') as mystuff:
    lines = mystuff.readlines()
    for line in lines:
        for post in d.entries:  
            if (post.title in line):
                print("Here is one: {0} {1}".format(post.title, post.link))

After that you can close the file too. Regarding point 2) the last for loop in parenthesis was completely wrong and give you multiple times the same output. Please let me know if this solve your exercise.

The line you find will work alone, it is a short way to compress everything in one line. Before you should only iterate on the posts:

for post in d.entries:
    if any(post.title in myline for myline in mylines):
        print("Here is one: {0} {1}".format(post.title, post.link))

where mylines is a mystuff.readlines(). Hope this help you in the understanding.

Marco smdm
  • 1,020
  • 1
  • 15
  • 25
  • this code returns nothing, although I have a matching title in mystuff.txt it also returns the title for the xml feed, one character per line. :) rss/xml is from here http://www.w3schools.com/xml/xml_rss.asp – J.D Joe Apr 07 '16 at 10:52
  • @J.D. Joe : Thanks for the input file. Thus try to use the lines = mystuff.readlines() and afterwards iterate on that. If mystuff is empty there will be no match of course!! I think you are trying to match some title in a .rss and afterwards printing Title and Link. Last but not least I tried within mystuff the world "XML Tutorial" and I got Here is one: XML Tutorial http://www.w3schools.com/xml . I hope this is what you expect, otherwise I misunderstood your query. Ciao and have a great one!! – Marco smdm Apr 07 '16 at 11:52
0
if any(post.title in myline for myline in mylines)

Does any element of mylines contain post.title ? True/False

See: https://docs.python.org/2/library/functions.html#any

edit:

post.title in myline for myline in mylines is a generator expression, which, to simplify, we can say is similar to a list.

Suppose post.title is foo and mylines is ['foo', 'bar', 'foobar', 'barbaz'].

[post.title in myline for myline in mylines] (casting the generator to a list, also known as a list comprehension), gives you

[
    True,   # 'foo' is in 'foo'
    False,  # 'foo' is not in 'bar'
    True,   # 'foo' is in 'foobar'
    False   # 'foo' is not in 'barbaz'
]

Now as per the docs I linked above, you can rewrite any as:

def any(iterable):
    for element in iterable:
        if element:
            return True
    return False

The only way that returns False is if all the elements in the iterable are falsy (for example, False, None, an empty list/dict/set/string etc, 0, etc).

Our iterable as shown above contains the value True, which is not a falsy value. Therefore, any returns True.

magni-
  • 1,934
  • 17
  • 20
  • +1 just to clarify, I got the idea for this line from http://stackoverflow.com/a/6531704/6170620 , it worked but I did not understand why. – J.D Joe Apr 07 '16 at 08:34
  • I edited my answer to try to make things clearer. Did that help? If not, what exactly are you confused about? – magni- Apr 07 '16 at 09:32