2016-08-22 6 views
0

私はPythonの初心者です。 私はこのXMLファイルのすべての要素を合計しようとしていますが、私のコードは何らかの理由でファイルの一部を集計しているようです。 私は理解しようとしましたが失敗しました。親切にアドバイスをお願いしますか?ありがとう。あなたの<comment>タグの 長いファイル申し訳ありません私のコードはXMLファイルの一部のみを解析するのはなぜですか?

import xml.etree.ElementTree as ET 

input=''' 
<commentinfo> 
<note>This file contains the sample data for testing</note> 
<comments> 
<comment> 
<name>Romina</name> 
<count>97</count> 
</comment> 
<comment> 
<name>Laurie</name> 
<count>97</count> 
</comment> 
<comment> 
<name>Bayli</name> 
<count>90</count> 
</comment> 
<comment> 
<name>Siyona</name> 
<count>90</count> 
</comment> 
<comment> 
<name>Taisha</name> 
<count>88</count> 
</comment> 
<comment> 
<name>Ameelia</name> 
<count>87</count> 
</comment> 
<comment> 
<name>Alanda</name> 
<count>87</count> 
</comment> 
<comment> 
<name>Prasheeta</name> 
<count>80</count> 
</comment> 
<comment> 
<name>Risa</name> 
<count>79</count> 
</comment> 
<comment> 
<name>Asif</name> 
<count>79</count> 
</comment> 
<comment> 
<name>Zi</name> 
<count>78</count> 
</comment> 
<comment> 
<name>Ediomi</name> 
<count>76</count> 
</comment> 
<comment> 
<name>Danyil</name> 
<count>76</count> 
</comment> 
<comment> 
<name>Barry</name> 
<count>72</count> 
</comment> 
<comment> 
<count>64</count> 
<name>Lance</name> 
<count>72</count> 
</comment> 
<comment> 
<name>Hattie</name> 
<count>66</count> 
</comment> 
<comment> 
<name>Mathu</name> 
<count>66</count> 
</comment> 
<comment> 
<name>Bowie</name> 
<count>65</count> 
</comment> 
<comment> 
<name>Samara</name> 
<count>65</count> 
</comment> 
<comment> 
<name>Uchenna</name> 
</comment> 
<comment> 
<name>Shauni</name> 
<count>61</count> 
</comment> 
<comment> 
<name>Georgia</name> 
<count>61</count> 
</comment> 
<comment> 
<name>Rivan</name> 
<count>59</count> 
</comment> 
<comment> 
<name>Kenan</name> 
<count>58</count> 
</comment> 
<comment> 
<name>Isma</name> 
<count>57</count> 
</comment> 
<comment> 
<name>Hassan</name> 
<count>57</count> 
</comment> 
<comment> 
<name>Samanthalee</name> 
<count>54</count> 
</comment> 
<comment> 
<name>Alexa</name> 
<count>51</count> 
</comment> 
<comment> 
<name>Caine</name> 
<count>49</count> 
</comment> 
<comment> 
<name>Grady</name> 
<count>47</count> 
</comment> 
<comment> 
<name>Anne</name> 
<count>40</count> 
</comment> 
<comment> 
<name>Rihan</name> 
<count>38</count> 
</comment> 
<comment> 
<name>Alexei</name> 
<count>37</count> 
</comment> 
<comment> 
<name>Indie</name> 
<count>36</count> 
</comment> 
<comment> 
<name>Rhuairidh</name> 
<count>36</count> 
</comment> 
<comment> 
<name>Annoushka</name> 
<count>32</count> 
</comment> 
<comment> 
<name>Kenzi</name> 
<count>25</count> 
</comment> 
<comment> 
<name>Shahd</name> 
<count>24</count> 
</comment> 
<comment> 
<name>Irvine</name> 
<count>22</count> 
</comment> 
<comment> 
<name>Carys</name> 
<count>21</count> 
</comment> 
<comment> 
<name>Skye</name> 
<count>19</count> 
</comment> 
<comment> 
<name>Atiya</name> 
<count>18</count> 
</comment> 
<comment> 
<name>Rohan</name> 
<count>18</count> 
</comment> 
<comment> 
<name>Nuala</name> 
<count>14</count> 
</comment> 
<comment> 
<name>Carlo</name> 
<count>12</count> 
</comment> 
<comment> 
<name>Maram</name> 
<count>12</count> 
</comment> 
<comment> 
<name>Japleen</name> 
<count>9</count> 
</comment> 
<comment> 
<name>Breeanna</name> 
<count>7</count> 
</comment> 
<comment> 
<name>Zaaine</name> 
<count>3</count> 
</comment> 
<comment> 
<name>Inika</name> 
<count>2</count> 
</comment> 
</comments> 
</commentinfo>''' 

tree = ET.fromstring(input) 
counts = tree.findall('comments/comment') 

summa=0 
for item in counts: 
    try: 
     k=item.find('count').text 
     k=int(k) 
     print k 
     summa +=k 
    except: 
     break 

print summa 

答えて

1

一つ何<count>を持っていない:

<comment> 
<name>Uchenna</name> 
</comment> 

これはNoneitem.find('count')になります。明らかに、Noneには.textという属性がないため、AttributeErrorが生成されます。広範な例外処理ではAttributeErrorがキャッチされ、ループが早期に終了します。

これは、あなたが使用してはならない理由の良いデモ:あなたはあなたが扱う(とする方法を知っているのみキャッチ例外は最小限としてtryスイートでコードを維持しようとする必要があり

try: 
    ... 
except: 
    ... 

できるだけ)。この場合:

for item in counts: 
    try: 
     k=item.find('count').text 
     k=int(k) 
    except (AttributeError, ValueError): # missing or malformatted `<count>`. 
     continue # Skip that tag and keep on summing the others 
    print k 
    summa +=k 
+0

これは非常に役に立ちます。ありがとうございました。 – Larry

関連する問題