Home > Net >  How to get just the value from dict
How to get just the value from dict

Time:10-19

I'm working on an already working code, and when i do this print statement, print(transaction_obj["group"]) i get

Transaction: 83a9bb1f808c464fbf21952abbfd8c0a

not sure if it's a dict, i just want to get 83a9bb1f808c464fbf21952abbfd8c0a this value when i print. I tried to print just the value by transaction_obj["group"][0] and transaction_obj["group"]['Transaction'], but it doesn't seem to work

CodePudding user response:

There could be different ways.

In Python you need to know the output type.

If this is a string, you can either:

output = string.split(':')[-1].lstrip()
#or 
output = string.split(': ')[-1]

#performance: 93.8 ns ± 1.54 ns per loop (mean ± std. dev. of 7 runs, 10,000,000 loops each)

if, and only if you guarantee that structure: Transaction: SPACE YOUR_DESIRED_RESULT

Or you can use regex

import re 
output = re.search(': (. ?)$',string).group(1)
#performance: 611 ns ± 9.2 ns per loop (mean ± std. dev. of 7 runs, 1,000,000 loops each)

CodePudding user response:

Try to check the type of value you want by using

print(type(transaction_obj["group"]))

If it is a string(most probably it is), you may use split() function in order to split the second part from the string by

print(transaction_obj["group"].split()[1])
  • Related