Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Print a part of stream-string?

How can I print what’s after a colon like ‘s’:, ‘E’:, or ‘p’: by giving the symbol as reference? I tried this based on all what I know so far but I get a weird error:

        XXXX = f"{{'stream': 'ABCDEF', 'data': {{'s': 'ABC', 'E': 1123, 'p': '0.0',  'q': '0.0'}}}}"
        print(XXXX)
        print(XXXX['data']['p'])

Output:

{'stream': 'ABCDEF', 'data': {'s': 'ABC', 'E': 1123, 'p': '0.0',  'q': '0.0'}}
Error: in main: print(XXXX['data']['p'])
       TypeError: string indices must be integers

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

By using ast module, you can simply do what you need:

import ast
XXXX = f"{{'stream': 'ABCDEF', 'data': {{'s': 'ABC', 'E': 1123, 'p': '0.0',  'q': '0.0'}}}}"
myDict = ast.literal_eval(XXXX)
print(myDict["data"]["p"])

Output

'0.0'
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading