Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7071549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:42:05+00:00 2026-05-28T05:42:05+00:00

I have a csv file being read into python, I then save the reader

  • 0

I have a csv file being read into python, I then save the reader as an array (I guess).

I then compare the csv file results against some Oracle db results:

readerSetSAP = []
readerSAP = csv.reader(StringIO.StringIO(request.POST['sap'].value),dialect=csv.excel)
readerSetSAP.extend(readerSAP)

empsTbl = meta.Session.query(model.Person).all();

Then use a nested loop to compare:

 if i.userid != currEmp[0].strip():
                        updated = True
                        print "userid update"

The problem is, I often have the warning:

eWarning: Unicode unequal comparison failed to convert both arguments to Unicode - interpreting them as being unequal

So my question is:

What is the most robust way of comparing strings of this type in Python?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:42:06+00:00Added an answer on May 28, 2026 at 5:42 am

    Your problem here is not ‘a robust way ” to compare strings. A robust way to compare strigns in Python is the equality operator == –
    Your problem is that your data is being covnerted to Unicode somewhere, without you being aware of that.

    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é”.

    Here is a Python console example of when the problem might happen:

    >>> "maçã" == u"maçã"
    __main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
    False
    

    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.

    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 –

    >>> "maçã".decode("utf-8") == u"maçã"
    True
    

    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”.

    I’d advise reading of this piece – it is rather useful:
    http://www.joelonsoftware.com/articles/Unicode.html

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a comma delimited CSV file being loaded into a String value generated
How to read and import .csv file in groovy on grails. I have .csv
I have my CSV file imported as such: records = FasterCSV.read(path, :headers => true,
I have CSV data loaded into a multidimensional array. In this way each row
I have this code which extracts data from a csv file and then reformats
I have a csv file with records being sorted on the first field .
I am importing a csv file into R using the sqldf -package. I have
I have a DataTable which is populated from a CSV file then, using a
I have CSV file and Macro in VBA. I want to open CSV file
I have a .csv file that is frequently updated (about 20 to 30 times

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.