Error while comparing string after transforming from datetime
<p>I am converting datetime to string and then comparing it with string from stdout.read from a file. But the "==" comparision fails.</p>

<p>Below is my code:</p>

<pre><code>date_input = datetime.datetime.now()
dat = date_input.strftime("%Y-%m-%d %H:%M:%S.%f")

command = 'echo %s &gt; /media/mmcblk0p1/testCard.txt' %dat
try:
    stdin, stdout, stderr = self._sshclient.exec_command(command,timeout=10)
    command = 'cat /media/mmcblk0p1/testCard.txt'
    try:
        stdin, stdout, stderr = self._sshclient.exec_command(command, timeout=10)
        date_output = stdout.read().decode(encoding='UTF-8')
        self.logger.debug("dat=%s date_output=%s ",dat,date_output)

        if(date_output == dat):    #FAILS
            self.logger.debug("read from file successful")
        else:
            self.logger.debug("read from file unsucessful")
    except:
        self.logger.exception('%s %s error, readtimeout',
                              ip, self.__class__.__name__)
        self.add_response("read error/timeout")
except :
    self.logger.exception('%s %s error, writetimeout',
                          ip, self.__class__.__name__)
    self.add_response("write error/timeout")
</code></pre>

<h2>Output</h2>

<pre><code>dat=2016-09-06 15:49:49.104030 date_output=2016-09-06 15:49:49.104030
read from file unsucessful
dat=2016-09-06 15:49:49.237551 date_output=2016-09-06 15:49:49.237551
read from file unsucessful
</code></pre>

<p>I am unable to figure out the comparision failure, it prints "read from file unsuccessful" Is it for the stdout.read() or datetime.</p>

<p>NOTE: There is no exception in this code, and the values are same if i use print method also.</p>

<p>Any solution is welcome</p>

<pre><code>&gt;&gt;&gt; a
'2016-09-06 15:49:49.104030'
&gt;&gt;&gt; b
u'2016-09-06 15:49:49.104030'
&gt;&gt;&gt; a == b
True
&gt;&gt;&gt; b = b + "\n"
&gt;&gt;&gt; a == b
False
</code></pre>

<p>There could be whitespace characters in one of your strings, remove them using .strip()</p>

python datetime