简体   繁体   中英

get value of a json/dict string

I have following string:

"root['bananas']['costs'][0]"

I want to get costs out of it in a reusable way -> the value I want to extract will always before the [0]. What is the best way to do this?

Well If you want to get the element before the last one (with no relation if its a zero or not) and you know that you would have quotes ' ' wrapped around the target word you could use something like this:

str = "root['bananas']['costs'][0]"

str.split("[")[-2].rstrip("']").lstrip("'")

Again if your output is more random than this solution will have to be edited.

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