2009-11-30 58 views
2

我有这样的脚本:Python中的Unicode:如何测试针对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包含unicode字符串和我想测试对命令行这些字符串:

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: 

我应该如何修改我的Unicode代码?

回答

1

尝试使用CP1252(Windows默认的Unicode)编码的Excel的Unicode字符串,然后再测试。我知道很多人不推荐这个,但这是有时解决我的问题。

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

溴。