2016-11-30 10 views
2

私が現在取り組んでいるデータ処理タスクがあります。2つのpythonデータ処理スクリプトを1つのワークフローに結合

私は別々の機能を実現する2つのPythonスクリプトを持っていますが、それらは同じデータで動作しますが、単一のワークフローに統合できると思いますが、これを達成する最も論理的な方法は考えられません。

データファイルはhereですが、JSONですが、2つの異なるコンポーネントがあります。

最初の部分は次のようになります。

  1: 6, 
     2: 20, 
     3: 2673, 
     4: 126, 
     5: 459, 
     6: 346, 
     8: 11, 
     9: 68, 
     10: 82, 

:この形式のデータをレンダリング

import json 
from collections import defaultdict 
from pprint import pprint 

with open('data-science.txt') as data_file: 
    data = json.load(data_file) 

locations = defaultdict(int) 

for item in data['data']: 
    location = item['relationships']['location']['data']['id'] 
    locations[location] += 1 

pprint(locations) 

{ 
    "links": { 
     "self": "http://localhost:2510/api/v2/jobs?skills=data%20science" 
    }, 
    "data": [ 
     { 
      "id": 121, 
      "type": "job", 
      "attributes": { 
       "title": "Data Scientist", 
       "date": "2014-01-22T15:25:00.000Z", 
       "description": "Data scientists are in increasingly high demand amongst tech companies in London. Generally a combination of business acumen and technical skills are sought. Big data experience ..." 
      }, 
      "relationships": { 
       "location": { 
        "links": { 
         "self": "http://localhost:2510/api/v2/jobs/121/location" 
        }, 
        "data": { 
         "type": "location", 
         "id": 3 
        } 
       }, 
       "country": { 
        "links": { 
         "self": "http://localhost:2510/api/v2/jobs/121/country" 
        }, 
        "data": { 
         "type": "country", 
         "id": 1 
        } 
       }, 

それはここで最初のPythonスクリプトでの勤務ですこれらは、ロケーション"id"と、そのロケーションに割り当てられているレコードの数です。

"included": [ 
    { 
     "id": 3, 
     "type": "location", 
     "attributes": { 
      "name": "Victoria", 
      "coord": [ 
       51.503378, 
       -0.139134 
      ] 
     } 
    }, 

このPythonのファイルによって処理される:

JSONオブジェクトの他の部分はこのようになり

import json 
from collections import defaultdict 
from pprint import pprint 

with open('data-science.txt') as data_file: 
    data = json.load(data_file) 

locations = defaultdict(int) 

for record in data['included']: 
    id = record.get('id', None) 
    name = record.get('attributes', {}).get('name', None) 
    coord = record.get('attributes', {}).get('coord', None) 
    print(id, name, coord) 

それはこの形式でデータを出力:

3 Victoria [51.503378, -0.139134] 
1 United Kingdom None 
71 data science None 
32 None None 
3 Victoria [51.503378, -0.139134] 
1 United Kingdom None 
1 data mining None 
22 data analysis None 
33 sdlc None 
38 artificial intelligence None 
39 machine learning None 
40 software development None 
71 data science None 
93 devops None 
63 None None 
52 Cubitt Town [51.505199, -0.018848] 

私が実際に気に入っているのは、最終的な出力は次のようになります:

3, Victoria, [51.503378, -0.139134], 2673 

ここで、2673は、最初のスクリプトからジョブカウントを参照します。

座標がない場合、たとえば、次のようになります。 [51.503378, -0.139134]私はそれを捨てることができます。

これらのスクリプトを組み合わせて出力することは可能でしょうが、私はそのような総合的な思想家ではなく、どうやってそれを行うのか分かりません。

すべての実際のプロジェクトファイルlive here

答えて

1

functionsを使用すると、両方のスクリプトが同じデータを処理した後に2つのスクリプトを組み合わせる1つの方法です。だから、あなたは処理ロジックブロックの各機能を確認する必要があり、その後、最終的に結果を組み合わせる:

import json 
from collections import defaultdict 
from pprint import pprint 

def process_locations_data(data): 
    # processes the 'data' block 
    locations = defaultdict(int) 
    for item in data['data']: 
     location = item['relationships']['location']['data']['id'] 
     locations[location] += 1 
    return locations 

def process_locations_included(data): 
    # processes the 'included' block 
    return_list = [] 
    for record in data['included']: 
     id = record.get('id', None) 
     name = record.get('attributes', {}).get('name', None) 
     coord = record.get('attributes', {}).get('coord', None) 
     return_list.append((id, name, coord)) 
    return return_list # return list of tuples 

# load the data from file once 
with open('data-science.txt') as data_file: 
    data = json.load(data_file) 

# use the two functions on same data 
locations = process_locations_data(data) 
records = process_locations_included(data) 

# combine the data for printing 
for record in records: 
    id, name, coord = record 
    references = locations[id] # lookup the references in the dict 
    print id, name, coord, references 

機能をより良い名前を持つことができますが、これはあなたが探している統一を達成しなければなりません。

+1

出力ファイルにパイプしてみると、UnicodeEncodeError: 'ascii'コーデックで8文字目の位置に文字「\ xfc」をエンコードできません:序数が範囲内にない(128) ' – CMorales

+0

これは入力データと関係があります。例えば次のように読むことができます:http://stackoverflow.com/questions/5760936/handle-wrongly-encoded-character-in-python-unicode-string しかし、あなたは単にファイルの代わりに最後のループで 'print'を実行します。 – sal

+0

これが元の質問に答える場合は、受け入れられた解決策としてマークしてください。 – sal

関連する問題