2012-04-30 60 views
3

我是在很長一段時間停留在這個錯誤:Python的類型錯誤:預期的字符緩衝區對象,個人誤解

TypeError: expected a character buffer object 

我只是明白我誤解了,這是一些有關的unicode字符串之間的區別和一個'簡單'的字符串,我試圖用一個「正常」字符串使用上面的代碼,而我不得不通過一個unicode。所以在字符串被破解之前僞造簡單的「u」:/ !!!

順便說一句TypeError對我來說很不清楚,現在依然如此。

請問,有人可以解釋我缺少什麼以及爲什麼「簡單」字符串不是「字符緩衝區對象」?

可以用下面的代碼(萃取和(c)從here:)再現

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 

回答

11

的問題是,一個字節串的translate方法是不同一個unicode字符串的translate方法。這裏的的非Unicode版本的文檔字符串:

S.translate(table [,deletechars]) -> string

Return a copy of the string S, where all characters occurring in the optional argument deletechars are removed, and the remaining characters have been mapped through the given translation table, which must be a string of length 256.

和這裏的Unicode版本:

S.translate(table) -> unicode

Return a copy of the string S, where all characters have been mapped through the given translation table, which must be a mapping of Unicode ordinals to Unicode ordinals, Unicode strings or None. Unmapped characters are left untouched. Characters mapped to None are deleted.

你可以看到,非Unicode版本的期待「長度爲256的字符串」,而非Unicode版本正在期待「映射」(即字典)。所以問題不在於你的unicode字符串是一個緩衝區對象,非unicode字符串不是 - 當然都是緩衝區 - 但是一個translate方法期望這樣一個緩衝區對象,另一個不是。

+0

謝謝!我對此非常瞭解甚少!因爲我沒有注意到它不是同一個對象:/ – user1340802

相關問題