简体   繁体   中英

Input as List of dictionary python

str_arr = input()
    d = "}, "

    for line in str_arr:
        new_str = [e + '},' for e in str_arr.split(d) if e]

    print(len(new_str))
    for i in new_str:
        print(i)

I want my code to accept a list of dictionaries but the input is a string and tried to make the input str_list to a list but my code adds }, at the end of the console output how can I remove it. 在此处输入图片说明

In the for loop, should you simply do:

new_str = [eval(e.strip()) for e in str_arr.split(",")]

I assume that str_arr is an input like {"A":1},{"B":2,"C":3} , in other words, a string of comma separated dictionaries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM