Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Your problem here is not 'a robust way " to compare strings. A robust way to compare strigns in Python is the equality operator <code>==</code> - Your problem is that your data is being covnerted to Unicode somewhere, without you being aware of that. </p> <p>You, and everyone else who writes code, should be aware that text is not ASCII - not in a post 1990 world. Even if all of your application is restricted to English only, and should never run in an internatiol environment, you are bound to find some non-ASCII characters in peoples names, or in words like "resumé". </p> <p>Here is a Python console example of when the problem might happen:</p> <pre><code>&gt;&gt;&gt; "maçã" == u"maçã" __main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal False </code></pre> <p>Python's CSV module do no authomatic conversion, and works with byte strigns (that is - strigns aready converted to some encoding) - which means that that result you are fetching from the DB is in Unicode. Probably your connection is using some default.</p> <p>To solve that, assuming the data in your database is correctly formatted (and you did not already lost character information during the insertion), is to decode the string read from the CSV file, using an explicit encoding - so that both are in unicode (Python's internal encoding agnostic) string format -</p> <pre><code>&gt;&gt;&gt; "maçã".decode("utf-8") == u"maçã" True </code></pre> <p>So, you do use the "decode" method on the string read form the CSV file in order to have a proepr conversion, before comparing it. If you are on Windows, use the "cp1251" for decoding., In any other mainstream (application) O.S. it should be "utf-8".</p> <p>I'd advise reading of this piece - it is rather useful: <a href="http://www.joelonsoftware.com/articles/Unicode.html" rel="nofollow">http://www.joelonsoftware.com/articles/Unicode.html</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload