2010-02-11 33 views
9
def getText(nodelist): 
    """Extracts the text between XML tags 

    I took this directly from http://docs.python.org/library/xml.dom.minidom.html. 
    For example, if I have a tag <Tag>525</Tag> this method returns me '525' 
    """ 
    rc = "" 
    for node in nodelist: 
     if node.nodeType == node.TEXT_NODE: 
      rc = rc + node.data 
    return rc 

給我IndentationError: unindent does not match any outer indentation level(蟒蛇)文檔字符串是造成壓痕錯誤

def getText(nodelist): 
    rc = "" 
    for node in nodelist: 
     if node.nodeType == node.TEXT_NODE: 
      rc = rc + node.data 
    return rc 

不。我所做的只是刪除文檔字符串評論。到底是怎麼回事?

+0

請注意,通常執行速度更快:rc = [],rc.append(node.data),return''.join(rc)。這是因爲你不需要每次都創建一個新的字符串。 – EOL 2010-02-11 10:33:33

+0

'return''.join(n.data for n在nodelist中,如果n.nodeType == n.TEXT_NODE)' – 2018-02-06 06:19:29

回答

12

您的文檔字符串以製表符開頭。讓您的代碼只使用空格來縮進(或僅限制表符),包括文檔字符串的縮進。

+0

相似問題:空行必須與周圍代碼具有相同數量的前導空白。 – cfi 2012-09-12 09:03:35

+0

然而他們聲稱Python是一個很好的初學者語言,適合青少年。 – 2016-10-05 03:34:04

+0

我知道很多人對Python有強烈的意見,但它仍然是非常適合初學者的語言! :-) – 2016-10-05 03:56:22

2

確保您沒有爲縮進混合空格和製表符