【问题标题】:Python TypeError: expected a character buffer object, personal misunderstandingPython TypeError: expected a character buffer object,个人误解
【发布时间】:2012-04-30 14:31:25
【问题描述】:

我被这个错误卡住了很长时间:

 TypeError: expected a character buffer object

我只是理解我误解了什么,这是关于 unicode 字符串和“简单”字符串之间的区别,我尝试将上述代码与“普通”字符串一起使用,而我必须通过 unicode一。所以在字符串中断执行之前忘记简单的“u”:/ !!!

顺便说一句,TypeError 对我来说非常不清楚,现在仍然如此。

请解释一下我缺少什么以及为什么“简单”字符串不是“字符缓冲区对象”?

您可以使用下面的代码进行复制(从here 提取和(c):)

def maketransU(s1, s2, todel=u""):
    """Build translation table for use with unicode.translate().

    :param s1: string of characters to replace.
    :type s1: unicode
    :param s2: string of replacement characters (same order as in s1).
    :type s2: unicode
    :param todel: string of characters to remove.
    :type todel: unicode
    :return: translation table with character code -> character code.
    :rtype: dict
    """
    # We go unicode internally - ensure callers are ok with that.
    assert (isinstance(s1,unicode))
    assert (isinstance(s2,unicode))
    trans_tab = dict( zip( map(ord, s1), map(ord, s2) ) )
    trans_tab.update( (ord(c),None) for c in todel )
    return trans_tab

#BlankToSpace_table = string.maketrans (u"\r\n\t\v\f",u"     ")
BlankToSpace_table = maketransU (u"\r\n\t\v\f",u"     ")
def BlankToSpace(text) :
    """Replace blanks characters by realspaces.

    May be good to prepare for regular expressions & Co based on whitespaces.

    :param  text: the text to clean from blanks.
    :type  text: string
    :return: List of parts in their apparition order.
    :rtype: [ string ]
    """
    print text, type(text), len(text)
    try:
        out =  text.translate(BlankToSpace_table)
    except TypeError, e:
        raise
    return out

# for SO : the code below is just to reproduce what i did not understand
dummy = "Hello,\n, this is a \t dummy test!"
for s in (unicode(dummy), dummy):
    print repr(s)
    print repr(BlankToSpace(s))

生产:

u'Hello,\n, this is a \t dummy test!'
Hello,
, this is a      dummy test! <type 'unicode'> 32
u'Hello, , this is a   dummy test!'
'Hello,\n, this is a \t dummy test!'
Hello,
, this is a      dummy test! <type 'str'> 32

Traceback (most recent call last):
  File "C:/treetaggerwrapper.error.py", line 44, in <module>
    print repr(BlankToSpace(s))
  File "C:/treetaggerwrapper.error.py", line 36, in BlankToSpace
    out =  text.translate(BlankToSpace_table)
TypeError: expected a character buffer object

【问题讨论】:

    标签: python unicode


    【解决方案1】:

    问题在于字节串的translate 方法与unicode 字符串的translate 方法不同。这是非 unicode 版本的文档字符串:

    S.translate(table [,deletechars]) -> 字符串

    返回字符串 S 的副本,其中出现的所有字符 在可选参数中删除字符被删除,并且 剩余的字符已通过给定的映射 翻译表,必须是长度为256的字符串。

    这是 unicode 版本:

    S.translate(table) -> unicode

    返回字符串 S 的副本,其中所有字符都已映射 通过给定的转换表,它必须是 Unicode 序数到 Unicode 序数、Unicode 字符串或无。 未映射的字符保持不变。映射到无的字符 被删除。

    您可以看到非 unicode 版本需要“长度为 256 的字符串”,而非 unicode 版本需要“映射”(即 dict)。所以问题不在于您的 unicode 字符串是缓冲区对象,而非 unicode 字符串不是 - 当然,两者都是缓冲区 - 而是一个 translate 方法期望这样一个缓冲区对象而另一个不是。

    【讨论】:

    • 谢谢!我离理解这一点还差得很远!因为我没有注意到它不是同一个对象:/
    猜你喜欢
    • 1970-01-01
    • 2012-02-22
    • 2021-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-12
    • 2016-10-27
    • 2021-02-25
    相关资源
    最近更新 更多