0

*New to Programming

Question: I need to use the below "Data" (two rows as arrays) queried from sql and use it to create the message structure below.

data from sql using fetchall()

Data = [[100,1,4,5],[101,1,4,6]]

##expected message structure

message = {
            "name":"Tom",
            "Job":"IT",
            "info": [
               {
                "id_1":"100",
                "id_2":"1",
                "id_3":"4",
                "id_4":"5"
                },
               {
                "id_1":"101",
                "id_2":"1",
                "id_3":"4",
                "id_4":"6"
                },  
             ]
}

I tried to create below method to iterate over the rows and then input the values, this is was just a starting, but this was also not working

def create_message(data)
    for row in data:
       {
        "id_1":str(data[0][0],
        "id_2":str(data[0][1],
        "id_3":str(data[0][2],
        "id_4":str(data[0][3],
       }

Latest Code

def create_info(data):
    info = []
    for row in data:
        temp_dict = {"id_1_tom":"","id_2_hell":"","id_3_trip":"","id_4_clap":""}
        for i in range(0,1):
            temp_dict["id_1_tom"] = str(row[i])
            temp_dict["id_2_hell"] = str(row[i+1])
            temp_dict["id_3_trip"] = str(row[i+2])
            temp_dict["id_4_clap"] = str(row[i+3])
        info.append(temp_dict)
    return info
8
  • Need to know how to create a structure and then insert data via loop while reading the values from db
    – viki
    Sep 27, 2021 at 21:07
  • Could you please describe what's your expected result from your entry data?
    – Christophe
    Sep 27, 2021 at 22:51
  • Actually, I am not sure how to create the message structure, I was just trying to create a method to loop through the rows and get value with indexes. But I don't know how to return the value from for loop to get the iterated details and then use that method to create the actual method. My method is not returning anything and I am not sure how to get required output
    – viki
    Sep 27, 2021 at 23:02
  • Basically you have data from db, you need to use python methods to create the "message" structure, values for name and job, I will get in params. I am concerned about how to use db output to construct message
    – viki
    Sep 27, 2021 at 23:04
  • No problem to me, but I'm just not sure to really understand what you are looking for (input/ output), thus a very concrete example is usually very helpful!
    – Christophe
    Sep 27, 2021 at 23:20

1 Answer 1

2

Edit: Updated answer based on updates to the question and comment by original poster.

This function might work for the example you've given to get the desired output, based on the attempt you've provided:

def create_info(data):
    info = []
    for row in data:
        temp_dict = {}
        temp_dict['id_1_tom'] = str(row[0])
        temp_dict['id_2_hell'] = str(row[1])
        temp_dict['id_3_trip'] = str(row[2])
        temp_dict['id_4_clap'] = str(row[3])
        info.append(temp_dict)
    return info

For the input:

[[100, 1, 4, 5],[101,1,4,6]]

This function will return a list of dictionaries:

[{"id_1_tom":"100","id_2_hell":"1","id_3_trip":"4","id_4_clap":"5"}, 
{"id_1_tom":"101","id_2_hell":"1","id_3_trip":"4","id_4_clap":"6"}]

This can serve as the value for the key info in your dictionary message. Note that you would still have to construct the message dictionary.

3
  • Thanks, Actually I gave the random keys (id_1) for this post, those are actually different and without any pattern, how to handle those separate keys ? like id_1_trick, id_2_hell etc.
    – viki
    Sep 28, 2021 at 0:29
  • I restructured the code a bit as per my requirement. Need to know if that is the correct way. I am not able to format it on the page to look pretty. Once this part is done, I will work on the whole message part
    – viki
    Sep 28, 2021 at 2:32
  • I have updated my answer based on your comments and the edit to your question. Sep 28, 2021 at 18:30

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.