Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.8k views
in Technique[技术] by (71.8m points)

Posting list to Json without square brackets in Python

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"
              }
         }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.”


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...