简体   繁体   English

Python Unicode:如何针对Unicode字符串进行测试

[英]Python unicode: how to test against unicode string

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: 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:

How should I modify my code for Unicode? 如何修改Unicode代码?

Python has a sequence type called unicode which will be useful here. Python有一个称为unicode的序列类型,在这里很有用。 These links contain more information to help you regarding this: 这些链接包含更多有关此方面的信息,以帮助您:

Try encoding the Excel unicode to string using cp1252 (windows default unicode) and then testing. 尝试使用cp1252(Windows默认unicode)将Excel unicode编码为字符串,然后进行测试。 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: ... 伪=> if sh.cell(i, 1).value.encode('cp1252') in argset: ...

Br. BR。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM