2

I have created a file parameter for my python script. My idea is read these parameters from a JSON file and then execute wherever I need.

here is the sample file:

{
"configfiles": [
        {
            "log": "/home/ubuntu/scripts/mtm-etl/etl-process-log",
            "connection":"/home/ubuntu/scripts/mtm-etl/etlconfig/mtm-connection",
            "query_redshift_library":"/home/ubuntu/scripts/mtm-etl/etlconfig/query_redshift_library"
        }
    ]
}

Ok, now I pretend to read these key, values in a for within my script to assign them to a variable then, here is my code:

 with open('etl-parameters','r') as userinfo:
        param = json.load(userinfo)

    for param,extraction in param.items():
        if ("ETL" in param):
            for item in extraction:
                process = item['etl-process']
                stored_query = item['stored-query']
        elif( "configfiles"  in param):
            for item in extraction:
                logfile = item['log'],
                connectionfile = item['connection'],
                query_redshift_library = item['query_redshift_library']

My issue is in the elif because for one variable is being assigned the right data type as a string but for some reason for the variables logfile and connectionfile it's assigning a tuple. I have catched up this in my debug:

enter image description here enter image description here

I appreciate your feedback

thanks

1 Answer 1

3

The error is caused by extraneous commas there:

            logfile = item['log'],
            connectionfile = item['connection'],

Remove them and you'll get your variables as str:

            logfile = item['log']
            connectionfile = item['connection']

As to why this is working this way: python interprets your lines ending with a comma as if you were indeed assigning single element tuples to your variables (parentheses are actually not required), like in this more simple example:

>>> a = 5,
>>> print(a)
(5,)
>>> print(str(type(a)))
<class 'tuple'>
>>>
Sign up to request clarification or add additional context in comments.

1 Comment

Damt it I was thinking on SQL when I was writing this code what a silly guy. Thanks man

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.