2009-11-30 7 views
2

J'ai un script comme ceci:Python unicode: comment tester contre chaîne unicode

#!/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 contient des chaînes unicode et je veux tester contre ces chaînes de la ligne de commande:

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: 

Comment modifier mon code pour Unicode?

Répondre

4

Python a un type de séquence appelé unicode qui sera utile ici. Ces liens contiennent plus d'informations pour vous aider à ce sujet:

+1

J'ai trouvé ce qui a résolu le problème: http://stackoverflow.com/questions/846850/how-to-read-unicode-characters-from-command-line-arguments-in-python-on- windows – jrara

+0

Le premier lien n'existe plus! – BajajG

1

Essayez codant pour la unicode Excel à l'aide de la chaîne CP1252 (fenêtres par défaut unicode), puis tester. Je sais que beaucoup de gens ne le recommandent pas, mais c'est ce qui résout parfois mes problèmes.

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

Br.