2010-03-31 19 views
0

我正在使用minidom解析xml文檔。我用yum標籤獲取數據並將它們存儲在一個列表中,並計算出單詞的頻率。但是,它不會將它們作爲列表中的字符串進行存儲或讀取。還有另一種方法可以做到嗎?現在,這是我有:將Doc對象轉換爲python中的字符串

yumNodes = [node for node in doc.getElementsByTagName("yum")] 

for node in yumNodes: 
    yumlist.append(t.data for t in node.childNodes if t.nodeType == t.TEXT_NODE) 

for ob in yumlist: 
    for o in ob: 
     if word not in freqDict: 
      freqDict[word] = 1 
     else: 
      freqDict[word] += 1 
+0

代替你可以發佈你的XML的一部分嗎? – YOU 2010-03-31 01:23:37

+0

如何列出您所看到的特定異常? – 2010-03-31 01:31:38

回答

1

不直接關係到你的問題,但因爲這會提高你的代碼的話...圖案

freqDict = {} 
... 
if word not in freqDict: 
    freqDict[word] = 1 
else: 
    freqDict[word] += 1 

通常與

import collections 
freqDict = collections.defaultdict(int) 
... 
freqDict[word] += 1 

或預2.5

freqDict = {} 
... 
freqDict.setdefault(word, 0) += 1 
0

改爲如下

yumlist.append(t.data for t in node.childNodes if t.nodeType == t.TEXT_NODE) 

yumlist.append(t.nodeValue for t in node.childNodes if t.nodeType == 3)