0

I have a string like :

"probability": {"go": 0.63549300785454799, "stand": 0.15739544829291019, "stop": 0.36450699214545207}, "label": "go"

Of this string, i want to extract only the part after '}, "', i.e.

"label": "go"

This string is generated in a loop, hence have to perform this action within the loop itself. Also, the length of the different parts of the string is not constant. How may I extract the desired part of the string?

3
  • you mean like s.split('}, ')[1]? Commented Oct 27, 2015 at 13:08
  • 5
    I can pretty much guarantee you're solving the wrong problem here. It looks like your string is a substring of some JSON document, which you should be handling using actual JSON tools. Commented Oct 27, 2015 at 13:09
  • @EdChum: thanks, it served the purpose. Commented Oct 27, 2015 at 13:17

1 Answer 1

1
thestring = '"probability": {"go": 0.63549300785454799, "stand": 0.15739544829291019, "stop": 0.36450699214545207}, "label": "go"'
print thestring.split('}, ')[1]

Output:

"label": "go"
Sign up to request clarification or add additional context in comments.

1 Comment

Looks good but print is a function in python 3. Use either print(thestring.split('}, ')[1]) or just plain thestring.split('}, ')[1].

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.