[英]How to convert a string with unicode in it to unicode using python
[英]Python unicode: how to test against unicode string
我有一個像這樣的腳本:
#!/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的序列類型,在這里很有用。 這些鏈接包含更多有關此方面的信息,以幫助您:
嘗試使用cp1252(Windows默認unicode)將Excel unicode編碼為字符串,然后進行測試。 我知道很多人不建議這樣做,但這有時可以解決我的問題。
偽=> if sh.cell(i, 1).value.encode('cp1252') in argset: ...
BR。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.