1

So I'm fairly new to Python but I have absolutely no idea why this strong oldUser is changing to current user after I make the parse call. Any help would be greatly appreciated.

while a < 20:
    f = urllib.urlopen("SITE")
    a = a+1

    for i, line in enumerate(f):
        if i == 187:
            print line

            myparser.parse(line)
            if fCheck == 1:
                result  = oldUser[0] is oldUser[1]
                print oldUser[0]
                print oldUser[1]
            else:
                result = user is oldUser

            fCheck = 1
            print result

            user = myparser.get_descriptions(firstCheck)

            firstCheck = 1
            print user

            if result:
                print "SAME"
                array[index+1] = array[index+1] +0
            else:
                oldUser = user

        elif i > 200:
            break
        myparser.reset()

I don't understand why result doesn't work either... I print out both values and when they're the same it's telling me they're not equal... Also, why does myparser.parse(line) turn oldUser into a size 2 array? Thanks!

** Here's the definition for myparse...

class MyParser(sgmllib.SGMLParser):
"A simple parser class."

def parse(self, s):
    "Parse the given string 's'."
    self.feed(s)
    self.close()

def __init__(self, verbose=0):
    "Initialise an object, passing 'verbose' to the superclass."

    sgmllib.SGMLParser.__init__(self, verbose)
    self.divs = []
    self.descriptions = []
    self.inside_div_element = 0

def start_div(self, attributes):
    "Process a hyperlink and its 'attributes'."

    for name, value in attributes:
        if name == "id":
            self.divs.append(value)
            self.inside_div_element = 1

def end_div(self):
    "Record the end of a hyperlink."

    self.inside_div_element = 0

def handle_data(self, data):
    "Handle the textual 'data'."

    if self.inside_div_element:
        self.descriptions.append(data)


def get_div(self):
    "Return the list of hyperlinks."

    return self.divs

def get_descriptions(self, check):
    "Return a list of descriptions."
if check == 1:
    self.descriptions.pop(0)
    return self.descriptions
1
  • Post all of your code. What you posted can't even run. Look at 'fCheck' vs. 'firstCheck'. Commented May 2, 2011 at 23:43

2 Answers 2

5

Don’t compare strings with is. That checks if they’re the same object, not two copies of the same string. See:

>>> string = raw_input()
hello
>>> string is 'hello'
False
>>> string == 'hello'
True

Also, the definition of myparser would be useful.

Sign up to request clarification or add additional context in comments.

1 Comment

Ahhhh i see... thank you! but do you know why saying user = myparse.get_descriptions() is also changing oldUser? myparse.get_descriptions() returns the user name that I want in taht line of HTML
1

I'm not quite sure what your code is doing, but I suspect you want to use == instead of is. Using is compares object identity, which is not the same as string equality. Two different string objects may contain the same sequence of characters.

result = oldUser[0] == oldUser[1]

If you're curious, for more information on the behaviour of the is operator see Python “is” operator behaves unexpectedly with integers.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.