Skip to content
Advertisement

Extract parameterized data from string using python from AWS Lambda EVENT

This is the raw string

"body": "----------------------------276035280975268320779229rnContent-Disposition: form-data; name="status"rnrn payment_received_unrecognisedrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="err"rnrn An incorrect bitcoin amount has been receivedrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="private_key_hash"rnrn sadf65d4sf56sd4f6sd54fsdfs....rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="box"rnrn 111rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="boxtype"rnrn criptoBoxrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="order"rnrn rn----------------------------6547984164*94652165481564rnContent-Disposition: form-data; name="user"rnrn ADAMrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="userformat"rnrn rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="usercountry"rnrn rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="period"rnrn rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="amount"rnrn 0.0016rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="amountusd"rnrn 152.45rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="coinlabel"rnrn BTCrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="coinname"rnrn bitcoinrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="addr"rnrn 14dt4cSbvwghDcETJDuvFGHe5bCsCPR1jWrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="tx"rnrn 6f1c6f34189a27446d18e25b9c79db90be55b0bb775b1768b5aa4520f23d71a8rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="confirmed"rnrn 0rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="timestamp"rnrn 1422623712rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="date"rnrn 30 January 2015rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="datetime"rnrn 2015-01-30 13: 15: 12rn----------------------------276035280975268320779229--rn"
    }

I need to fetch value in DICT format, like

{
'status': 'payment_received_unrecognised',
'boxtype': 'Criptobox'
}

I am unable to extract data as i wanted… i need suggestion how to extract data from the string?

N.B: here string like “276035280975268320779229” changes every time i send request.

Advertisement

Answer

Here is an option using re.findall:

inp = '"body": "----------------------------276035280975268320779229rnContent-Disposition: form-data; name="status"rnrn payment_received_unrecognisedrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="err"rnrn An incorrect bitcoin amount has been receivedrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="private_key_hash"rnrn sadf65d4sf56sd4f6sd54fsdfs....rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="box"rnrn 111rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="boxtype"rnrn criptoBoxrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="order"rnrn rn----------------------------6547984164*94652165481564rnContent-Disposition: form-data; name="user"rnrn ADAMrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="userformat"rnrn rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="usercountry"rnrn rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="period"rnrn rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="amount"rnrn 0.0016rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="amountusd"rnrn 152.45rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="coinlabel"rnrn BTCrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="coinname"rnrn bitcoinrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="addr"rnrn 14dt4cSbvwghDcETJDuvFGHe5bCsCPR1jWrn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="tx"rnrn 6f1c6f34189a27446d18e25b9c79db90be55b0bb775b1768b5aa4520f23d71a8rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="confirmed"rnrn 0rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="timestamp"rnrn 1422623712rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="date"rnrn 30 January 2015rn----------------------------276035280975268320779229rnContent-Disposition: form-data; name="datetime"rnrn 2015-01-30 13: 15: 12rn----------------------------276035280975268320779229--rn"'
my_dict = dict(re.findall(r'bname="(status|boxtype)"s+(S+)', inp))

print(my_dict)

This prints:

{'status': 'payment_received_unrecognised', 'boxtype': 'criptoBox'}
Advertisement