Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to run python script
#1
I need to run a python script written by a previous developer.
When I run it on my Mac, I get the following
File "health_json_to_csv.py", line 31
output = write_with_prefix(input[0], output, f"Wages{prefix}Annual")
The script performs API calls to populate a csv file from a database. Here is the code minus the API keys:
import requests
import json
import csv
import time

ID = "xxxxxxxxxxxxxxxxxxxxxxxx"
headers = {'Authorization': "api key"}

def write_with_prefix(input, output, prefix):
    """ iterate over input dict; flatten data to output dict with prefix """
    for item in input.keys():
        if input[item] is not None:
            ret_key = prefix + item
            output[ret_key] = input[item].strip()
    return output

def flatten_wages(input, output, prefix):
    """ take national/state/area wages; strip unneeded data and flatten remainder """
    rate_type = input[0]["RateType"]
    del input[0]["RateType"]
    del input[0]["StFips"]
    del input[0]["Area"]
    if len(input) > 1:
        del input[1]["RateType"]
        del input[1]["StFips"]
        del input[1]["Area"]

    if rate_type == "Annual":
        output = write_with_prefix(input[0], output, f"Wages{prefix}Annual")
        if len(input) > 1:
            output = write_with_prefix(input[1], output, f"Wages{prefix}Hourly")
    else:
        output = write_with_prefix(input[0], output, f"Wages{prefix}Hourly")
        if len(input) > 1:
            output = write_with_prefix(input[1], output, f"Wages{prefix}Annual")
    
    return output

def flatten_ed_type(input, output):
    """ Flatten education type dict in input to output dict """
    matrix = {
        "Less than high school diploma": "EdLessThanHighSchool",
        "High school diploma or equivalent": "EdHighSchoolDiploma",
        "Some college, no degree": "EdSomeCollege",
        "Associate's degree": "EdAssociatesDegree",
        "Bachelor's degree": "EdBachelorsDegree",
        "Master's degree": "EdMastersDegree",
        "Doctoral or professional degree": "EdDoctoralOrProfessionalDegree"
    }
    for item in input:
        key = matrix[item["EducationLevel"]] + "Pct"
        output[key] = item["Value"]

    return output

def flatten_projections(input, output):
    """ flatten state and national projections from input dict into output dict """
    for data in input:
        if data["StateName"] == "Washington":
            prefix = "State"
        else:
            prefix = "National"
        del data["Stfips"]
        del data["StateName"]
        output[prefix+"CurrentEstimatedEmployment"] = data["EstimatedEmployment"].strip()
        del data["EstimatedEmployment"]
        write_with_prefix(data, output, prefix)
    return output

def prepare_for_write(data):
    """ recieve a single occupational detail, manually flatten for csv translation"""
    ret = {}
    # flatten wage data
    if len(data["Wages"]["NationalWagesList"]) > 0:
        ret = flatten_wages(data["Wages"]["NationalWagesList"], ret, "National")   
    if len(data["Wages"]["StateWagesList"]) > 0:    
        ret = flatten_wages(data["Wages"]["StateWagesList"], ret, "State")
    if len(data["Wages"]["BLSAreaWagesList"]) > 0:
        ret = flatten_wages(data["Wages"]["BLSAreaWagesList"], ret, "Area")
    ret["WageYear"] = data["Wages"]["WageYear"]

    #flatten education
    education = data["EducationTraining"]
    ret = flatten_ed_type(education["EducationType"], ret)
    
    #flatten projections
    projections = data["Projections"]
    ret["ProjectedYear"] = projections["ProjectedYear"]
    ret = flatten_projections(projections["Projections"], ret)

    # remove unneeded top-level items
    del data["COSVideoURL"]
    del data["RelatedOnetTitles"]
    del data["TrainingPrograms"]
    del data["Wages"]
    del data["Projections"]
    del data["EducationTraining"]
    del data["SocInfo"]
    del data["AlternateTitles"]
    del data["StFips"]
    del data["Location"]
    ret = write_with_prefix(data, ret, '')

    return ret
with open('health-codes') as f:
    with open('data.csv', 'w') as csvfile:
        # good to find a way to autogen these
        fieldnames = [ "OnetTitle","OnetCode","OnetDescription",
            "WagesNationalAnnualPct10", "WagesNationalAnnualPct25",
            "WagesNationalAnnualMedian","WagesNationalAnnualPct75",
            "WagesNationalAnnualPct90","WagesNationalAnnualAreaName",
            "WagesNationalHourlyPct10","WagesNationalHourlyPct25",
            "WagesNationalHourlyMedian","WagesNationalHourlyPct75",
            "WagesNationalHourlyPct90","WagesNationalHourlyAreaName",
            "WagesStateAnnualPct10","WagesStateAnnualPct25",
            "WagesStateAnnualMedian","WagesStateAnnualPct75",
            "WagesStateAnnualPct90","WagesStateAnnualAreaName",
            "WagesStateHourlyPct10","WagesStateHourlyPct25",
            "WagesStateHourlyMedian","WagesStateHourlyPct75",
            "WagesStateHourlyPct90","WagesStateHourlyAreaName",
            "WagesAreaAnnualPct10","WagesAreaAnnualPct25",
            "WagesAreaAnnualMedian","WagesAreaAnnualPct75",
            "WagesAreaAnnualPct90","WagesAreaAnnualAreaName",
            "WagesAreaHourlyPct10","WagesAreaHourlyPct25",
            "WagesAreaHourlyMedian","WagesAreaHourlyPct75",
            "WagesAreaHourlyPct90","WagesAreaHourlyAreaName",
            "WageYear","EdLessThanHighSchoolPct",
            "EdHighSchoolDiplomaPct","EdSomeCollegePct",
            "EdAssociatesDegreePct","EdBachelorsDegreePct",
            "EdMastersDegreePct","EdDoctoralOrProfessionalDegreePct",
            "ProjectedYear","StateCurrentEstimatedEmployment",
            "StateProjectedEmployment","StatePerCentChange",
            "StateProjectedAnnualJobOpening","NationalCurrentEstimatedEmployment",
            "NationalProjectedEmployment","NationalPerCentChange",
            "NationalProjectedAnnualJobOpening","BrightOutlook","Green",
            "BrightOutlookCategory"]
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        writer.writeheader()
        for line in f:
            url = 'https://api.careeronestop.org/v1/occupation/' + ID + '/'+ line.strip() + '/98107?projectedEmployment=true&training=true&wages=true'
            print (url)
            response = requests.get(url, headers=headers)
            data = json.loads(response.content)
            prepared_data = prepare_for_write(data["OccupationDetail"][0])
            writer.writerow(prepared_data)
The file, 'health-codes' is in the same folder as the python script. Thank you for your assistance.

Looking at the database where the data will be imported from the CSV vile, I think the prefixes could be 'National' and 'State' or perhaps 'Area'. I have tried running the script with these but no success so far.
Quote
#2
If I enter python health_json_to_csv.py national state area into the terminal I get 'SyntaxError: invalid syntax'.
Quote
#3
probably you have python2 and python3 versions installed
you need to run this code with python 3.6 or higher
try
python3 health_json_to_csv.py
Quote
#4
Thank you. That works.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  crontab on RHEL7 not calling python script wrapped in shell script benthomson 1 139 Yesterday, 05:27 PM
Last Post: micseydel
  Package python script which has different libraries as a single executable or script tej7gandhi 1 545 May-11-2019, 08:12 PM
Last Post: keames
  Check Python version from inside script? Run Pythons script in v2 compatibility mode? pstein 2 6,494 Jul-07-2017, 08:59 AM
Last Post: snippsat
  Cant pass corect variables to python script in bash script neradp 3 2,898 Nov-05-2016, 01:26 AM
Last Post: Larz60+

Forum Jump:


Users browsing this thread: 1 Guest(s)