Using urllib2 to download text to be used to evaluate a variable
<p>Am downloading a text page, c.txt, from a webserver.  c.txt only contains the letter 'c' .  Able to download file fine and print its contents, the character 'c'.  Cannot, however, use its contents in the code below:</p>

<pre><code>import urllib2

req = urllib2.Request('http://localhost/c.txt')
response = urllib2.urlopen(req)
result = str(response.read())
print(result) # prints 'c' just fine
furl = "c"
furl = str(furl)
if result == furl: # Does not work
     print('Correct')  
</code></pre>

<p>No errors are incurred. Just will not work</p>

<p>There is most likely an extra white space character in the <code>result</code>. You can check for it by printing characters on either side.</p>

<pre><code>print '&gt;%s&lt;' % result
</code></pre>

<p>If the result looks like <code>&gt;c &lt;</code> you've got a trailing white space (including newline characters).</p>

<p>To remove the leading and trailing white space from a string, you can use the <a href="https://docs.python.org/2/library/string.html#string.strip" rel="nofollow"><code>strip()</code></a> method on the string.</p>

<pre><code>result = str(response.read()).strip()
</code></pre>

<p>Or you can do it right at the point of comparison.</p>

<pre><code>if result.strip() == furl:
    print 'correct'
</code></pre>

python urllib2