Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 How to run python script
I need to run a python script written by a previous developer.
When I run it on my Mac, I get the following
File "", 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")
        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"
            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",
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        for line in f:
            url = '' + 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])
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.
If I enter python national state area into the terminal I get 'SyntaxError: invalid syntax'.
probably you have python2 and python3 versions installed
you need to run this code with python 3.6 or higher
Thank you. That works.

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)