Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm trying to pass 10 country names at once to my payload by converting it to list. But i'm facing problems while executing the program as list comes with [square braces] that i'm unable to remove.

How can i send an entire list without square brackets to my payload. I tried json.dumps(payload) it's still not leaving the square braces.

format of my payload
payload= {"world" :
              {"continent": [
                              {"country": "HongKong"}
                            ],
                "planet": "earth"
              }
         }
my file-
file.csv
country
HongKong
USA
UK

how i expect the output to be-
payload= {"world" :
              {"continent": [
                              {"country": "HongKong"},{"country":"USA"}, 
                                                        {"country":"UK"}
                            ],
                "planet": "earth"
              }
         }

what i'm currently getting-
payload= {"world" :
              {"continent": [
                              {"country": ["HongKong","USA","UK"]}
                            ],
                "planet": "earth"
              }
         }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
4.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

What you are trying to do is not possible as that is a dictionary and it can only have one value and one key, the key in your case in Country, the value can only be one item hence why it is passed as a list. What you can do however is turn your list into a single string with the .join() method, maybe something along the lines of “, “.join(name_of_list). Your dictionary will then have the following entry: “Country”: “Hong Kong, USA, etc.”


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...