将unicode输入转换为字符串进行比较

2024-09-28 22:19:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在写的代码,它解析word文档表并与一个ascii字符串的关键字进行比较

tyring = unicode((ListTables[0].Rows[x])).encode('utf-8')
tryingstring = tyring.encode('ascii')
print 'trying string' ,tryingstring

错误如下:

^{pr2}$

但它不打印它应该,因为尝试字符串现在是一个ascii字符串?在


Tags: 字符串代码文档asciiunicode关键字utfword
3条回答

转换为Unicode字符串,并在转换后使用encode(),然后对其进行转换:

if tr1_find.search(unicode(ListTables[0].Cell(x,y)).encode('utf-8')):

回到你原来的帖子:

if tr1_find.search(str(ListTables[0].Cell(x,y))):
    print 'Found'
    value  = ListTables[0].Cell(x,y+1)

ListTables[0].Cell(x,y)从Word文档返回一个Cell实例。对其调用str()将检索其Unicode值,并尝试使用ascii编解码器将其编码为字节字符串。因为它包含非ASCII字符,所以失败时会出现UnicodeEncodingError。在

在以后的编辑中:

^{pr2}$

unicode检索Unicode值,将其转换为UTF-8字节字符串,并将其存储在tyring中。下一行尝试将字节字符串再次编码为“ascii”。这是无效的,因为只能对Unicode字符串进行编码,因此Python首先尝试使用默认的“ascii”编解码器将字节字符串转换回Unicode字符串。这将导致UnicodeDecodingError(不是编码)。在

最佳实践是用Unicode进行所有字符串处理。缺少的是Range()方法来获取单元格的值。下面是一个访问Word文档表的示例:

PythonWin 2.7.1 (r271:86832, Nov 27 2010, 18:30:46) [MSC v.1500 32 bit (Intel)] on win32.
Portions Copyright 1994-2008 Mark Hammond - see 'Help/About PythonWin' for further copyright information.
>>> import win32com.client
>>> word=win32com.client.gencache.EnsureDispatch('Word.Application')
>>> word.ActiveDocument.Tables[0].Cell(1,1).Range()
u'One\u4e00\r\x07'

请注意,它是一个Unicode字符串。Word似乎还使用\r\x07作为细胞系终止符。在

现在可以测试该值:

>>> value = word.ActiveDocument.Tables[0].Cell(1,1).Range()
>>> value == 'One'   # NOTE: Python converts byte strings to Unicode via the default codec ('ascii' in Python 2.X)
False
>>> value == u'One'
False
>>> value == u'One马\r\x07'
False
>>> value == u'One一\r\x07'
True
>>> value == u'One\u4e00\r\x07'
True
>>> value == 'One\x92' # non-ASCII byte string fails to convert
__main__:1: UnicodeWarning: Unicode equal comparison failed to convert both arguments to Unicode - interpreting them as being unequal
False

试试这个,我想知道它是否有用:

if tr1_find.search(unicode(ListTables[0].Cell(x,y)).encode('utf-8','ignore')):

您还可以从Python的文档中找到这个页面,它很有帮助: http://docs.python.org/howto/unicode.html

它涵盖了这类问题。在

相关问题 更多 >