2016-11-30 4 views
2

Es gibt eine Datenverarbeitungsaufgabe, an der ich gerade arbeite.Kombinieren Sie zwei Python-Datenverarbeitungsskripts in einem einzigen Arbeitsablauf

Ich habe zwei Python-Skripte, von denen jede eine separate Funktion erreicht, aber sie arbeiten auf den gleichen Daten, ich denke, sie können in einem einzigen Arbeitsablauf kombiniert werden, aber ich kann nicht den logischsten Weg vorstellen, dies zu erreichen .

Die Datendatei ist here, es JSON, aber es hat zwei verschiedene Komponenten.

Der erste Teil sieht wie folgt aus:

{ 
    "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 
        } 
       }, 

Es wird von der ersten Python-Skript gearbeitet, hier:

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) 

, die Daten dieser Form macht:

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

Dies sind der Standort "id" s und die Anzahl der Datensätze, die diesem Standort zugewiesen sind.

Der andere Teil des Objekts JSON sieht wie folgt aus:

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

und wird durch diese Python-Datei verarbeitet:

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) 

es Daten gibt in diesem Format:

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] 

Was ich eigentlich möchte, ist, dass die endgültige Ausgabe in etwa so aussehen würde:

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

Dabei bezieht sich 2673 auf die Auftragsanzahl aus dem ersten Skript.

Wenn es keine Koordinaten hat, z.B. [51.503378, -0.139134] Ich kann es wegwerfen.

Ich bin sicher, dass es möglich wäre, diese Skripte zusammen und erhält, dass Ausgabe zu kombinieren, aber ich bin nicht so ein umfassender Denker und ich kann nicht herausfinden, wie es zu tun.

Alle echten Projektdateien live here.

Antwort

1

functions Verwendung ist eine Möglichkeit, die beiden Skripte zu kombinieren, nachdem sie alle die gleichen Daten verarbeiten. Also, sollten Sie eine Funktion für jede der Verarbeitungslogikblöcke machen, und dann die Ergebnisse am Ende kombinieren:

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 

Die Funktion könnte besser Namen haben, aber die Einigung erzielen sollten Sie suchen.

+1

Das Skript funktioniert, aber wenn Sie versuchen, es in eine Ausgabedatei zu pipettieren, erhalten Sie den Fehler 'UnicodeEncodeError:' ascii 'Codec kann das Zeichen u' \ xfc 'nicht an Position 8 codieren: Ordinalwert nicht im Bereich (128) ' – CMorales

+0

Das hat mit den Eingabedaten zu tun. Es kann behandelt werden, lesen Sie hier zum Beispiel: http://stackoverflow.com/questions/5760936/handle-wrongly-encoded-character-in-python-unicode-string Aber Sie könnten auch nur in Datei speichern statt zu verwenden das 'print' in der letzten Schleife. – sal

+0

Wenn dies die ursprüngliche Frage beantwortet, markieren Sie sie bitte als akzeptierte Lösung. – sal

Verwandte Themen