私が現在取り組んでいるデータ処理タスクがあります。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。
出力ファイルにパイプしてみると、UnicodeEncodeError: 'ascii'コーデックで8文字目の位置に文字「\ xfc」をエンコードできません:序数が範囲内にない(128) ' – CMorales
これは入力データと関係があります。例えば次のように読むことができます:http://stackoverflow.com/questions/5760936/handle-wrongly-encoded-character-in-python-unicode-string しかし、あなたは単にファイルの代わりに最後のループで 'print'を実行します。 – sal
これが元の質問に答える場合は、受け入れられた解決策としてマークしてください。 – sal