【问题标题】:Python unicode: how to test against unicode stringPython unicode:如何针对 unicode 字符串进行测试
【发布时间】:2009-11-30 07:07:54
【问题描述】:

我有一个这样的脚本:

#!/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 代码?

【问题讨论】:

    标签: python unicode


    【解决方案1】:

    Python 有一个称为 unicode 的序列类型,这在这里很有用。这些链接包含更多信息以帮助您解决此问题:

    【讨论】:

    【解决方案2】:

    尝试使用 cp1252(windows 默认 unicode)将 Excel unicode 编码为字符串,然后进行测试。我知道很多人不建议这样做,但这有时可以解决我的问题。

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

    兄弟。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-19
      • 2014-04-16
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      • 1970-01-01
      • 2011-11-12
      相关资源
      最近更新 更多