2016-09-08 5 views
0

私は3つの異なるデータセットからデータを抽出し、それを組み合わせてキャンペーンの貢献度を調べるプロジェクトを行っています。これを行うために、関連するデータを2つのセットから辞書(canDictとotherDict)にID番号をキーにして必要な情報(パーティー所属)を値として設定しました。それから、私はキーに基づいてパーティー情報を引き出すためのプログラムを書いた(私の3番目のセットにはこれらのID番号も含まれています)、それらを寄付パーティーの雇用主とマッチさせました。それは長らく説明されていませんでしたが、このコードの理解に役立つと思いました。他の辞書からデータを取り出すことによって辞書をコンパイルする

私の問題は、何らかの理由で、私の3番目の辞書(employerDict)がコンパイルされないということです。このステップが終わるまでに、私は雇用者をキーとするタプルと値としてのタプルのリストを持っているべきですが、それを実行した後は辞書は空白のままです。私はこの行を十数回回り、私の髪を引っ張っている - 私の人生はなぜそれがうまくいかないと思うことができないので、答えを探すのが難しくなっている。私は文脈から理解しやすくするために、ほとんどすべての行にコメントしました。 誰かが私の間違いを見つけられますか?

アップデート:私はプログラムがすべてで反復処理されたかどうかを確認するためにループの最も外側に、カウンターを追加し、N。

アップデート2:はI [0] canDictまたはotherDictに存在しなかった場合には、可変partyの作成のデータにIDを別のif文を追加しました。コメントから既に提案されている修正を追加しました。

n=0 
with open(path3) as f:       # path3 is a txt file 
    for line in f: 
     n+=1 
     if n % 10000 == 0: 
      print(n) 
     data = line.split("|")     # Splitting each line into its entries (delimited by the symbol |) 
     party = canDict.get(data[0])   # data[0] is an ID number. canDict and otherDict contain these IDs as keys with party affiliations as values 
     if party is None: 
      party = otherDict[data[0]]   # If there is no matching ID number in canDict, search otherDict 
      if party is None: 
       party = 'Other' 
     else: 
      print('ERROR: party is None') 
     x = (party, int(data[14]))    # Creating a tuple of the the party (found through the loop) and an integer amount from the file path3 
     employer = data[11]     # Index 11 in path3 is the employer of the person 
     if employer != '': 
      value = employerDict.get(employer) # If the employer field is not blank, see if this employer is already a key in employerDict 
      if value is None: 
       employerDict[employer] = [x] # If the key does not exist, create it and add a list including the tuple x as its value 
      else: 
       employerDict[employer].append(x) # If it does exist, add the tuple x to the existing value 
     else: 
      print('ERROR: employer == ''') 
+0

私の直感は 'employer!= '''は常にfalseと評価されます。 2番目のif文の前に 'print(employer!= '')'を試してください。 –

+2

あなたの5行目で、 'data [0]'は 'None'(' split'に由来)である理由はありません。ですから、5行目はおそらく 'もしパーティーがNoneならば'でしょう。 –

+0

@AndrewJenkins考えてくれてありがとうございます:/私はカウンタを追加していました。ちょうど反復していて(更新を参照)、ゼロの出力も返しています。 – boop

答えて

0

すべての人のためにありがとう - しかし、それは私のデータファイルの問題ではなく、プログラムの問題ではないようです。 Dangit。

0

たちは少し簡素化し、物事を並べ替えるとします

import sys 
from collections import defaultdict 

employerDict = defaultdict(list) 

ID, EMPLOYER, AMOUNT = 0, 11, 14 

with open(path3) as f: # path3 is a *.txt file 

    for n, line in enumerate(f): 
     if n % 10000 == 0: 
      print(n) 

     data = line.rstrip().split('|') # Splitting each line into its entries 

     employer = data[EMPLOYER] # the employer of the person 

     if employer == '': # verify that you get 'employer' occasionally! 
      print("ERROR: employer == ''", file=sys.stderr) 
      continue 

     id_string = data[ID] # is ID always a string or is it an int (e.g. in other dicts)? 

     # If there is no matching ID number in canDict, search otherDict 
     party = canDict.get(id_string, otherDict.get(id_string, 'Other')) 

     # Create a tuple of the the party (found through the loop) and an integer amount from the file path3 
     x = (party, int(data[AMOUNT])) 

     employerDict[employer].append(x) # Add the tuple x to the (automatically) existing list 

は、このヘルプをしていますか?

+0

修正していただきありがとうございます - それは私の疑惑に私のデータファイルや私のpythonコンソールではなく、プログラムの問題だという証拠を増やしています。もう少し実験するとどちらが表示されます!ありがとう! – boop

関連する問題