1

So I am trying to find a string from a file. My code looks like this:

fname=open('results', 'r')
lines=fname.readlines()
for i in lines:
      print i 
      s=lines[41]
      x= "0x80000680: 0x00000000\n"

      if (i == x) :
         stuff happens

My code reads the file just finds the line. that matches 'x' but it does not go into the if statement. A thing a noticed that when it prints out the line in the output it seems to have more spaces, but when looking at the variable it self it only has one. I have tried to put in the same amount of spaces for both results, but I am still not able to go into the if statement. Here is the output of 'i' when I print it when it gets to that line: 0x80000680: 0x00000000 and it appears as : str: 0x80000680: 0x00000000\n when I look at the variables. If I look at x in the variables it shows str: 0x80000680: 0x00000000\n

7
  • 3
    Try print repr(i). My best guess would be that the actual line in question is "0x80000680:\t0x00000000\n" or something similar. Commented Jul 14, 2016 at 15:51
  • 1
    Spaces are characters too. If it isn't going into the if that means it isn't the same line as x. You could consider using regex (though it may be overkill) to only match the parts of the line you want. Commented Jul 14, 2016 at 15:51
  • wouldnt it show special characters such as \t somce it shows \n? @smarx Commented Jul 14, 2016 at 15:53
  • @kirkpatt I have tried to match the spaces on both and it doesnt seem to work Commented Jul 14, 2016 at 15:55
  • 1
    @NikitaBelooussov I'm not sure I understand your question, but print repr(i) should show you exactly what to use on your line x = .... Commented Jul 14, 2016 at 15:55

1 Answer 1

2

your line in your file is not "0x80000680: 0x00000000\n"

its easy to prove your line is not this

y="0x80000680: "+ "0x00000000\n" #ensure both x and y have different `id`
x= "0x80000680: 0x00000000\n"
print "ID:",id(x),id(y)
print y == x , y.strip() == x.strip()

as suggested you should print(repr(i)) to actually see what your line really looks like

or you could try just checking if it startswith

i.startswith("0x80000680:")

or you could try using re to sort of match

re.match("0x80000680:.*0x00000000",i)
Sign up to request clarification or add additional context in comments.

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.