Python unicode: how to test against unicode string

12,738

Solution 1

Python has a sequence type called unicode which will be useful here. These links contain more information to help you regarding this:

Solution 2

Try encoding the Excel unicode to string using cp1252 (windows default unicode) and then testing. I know a lot of people don't recommend this, but this is what sometimes solve my problems.

Pseudo=> if sh.cell(i, 1).value.encode('cp1252') in argset: ...

Br.

Share:
12,738
jrara
Author by

jrara

Updated on June 04, 2022

Comments

  • jrara
    jrara over 1 year

    I have a script like this:

    #!/Python26/
    # -*- coding: utf-8 -*-
    
    import sys
    import xlrd
    import xlwt
    
    argset = set(sys.argv[1:])
    
    #----------- import ----------------
    wb = xlrd.open_workbook("excelfile.xls")
    
    #----------- script ----------------
    #Get the first sheet either by name
    sh = wb.sheet_by_name(u'Data')
    
    hlo = []
    
    for i in range(len(sh.col_values(8))):
       if sh.cell(i, 1).value in argset:
            if sh.cell(i, 8).value == '':
                continue
            hlo.append(sh.cell(i, 8).value)
    

    excelfile.xls contains unicode strings and I want to test against these strings from command line:

    C:\>python pythonscript.py päätyö
    pythonscript.py:34: UnicodeWarning: Unicode equal comparison failed to convert both arguments to
    icode - interpreting them as being unequal
      if sh.cell(i, 1).value in argset:
    

    How should I modify my code for Unicode?

  • jrara
    jrara almost 14 years
    I found this, which solved the problem: stackoverflow.com/questions/846850/…
  • BajajG
    BajajG over 8 years
    The first link does not exist anymore!