0

I have data like this: {"address": "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK", "balance": 1234, "pending_balance": 0, "paid_out": 0}

I want extract numbers after balance, but its can be from 0 to infinity.

So, from example above the output desired:

1234

And btw one more question. I have got data like this {"address": "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK", "invoice": "invNrKU2ZFMuAJKUiejyVe3X34ybP9awyWZBfUEdY2dZKxYTB8ajW", "redeem_code": "BTCvQDD9xFYHHDYNi1JYeLY1eEkGFBFB49qojETjLBZ2CVYyPm56B"} Whats the normal way of doing that:

strs = repr(s)  
address = s[13:47]
invoice = s[62:115]
redeem_code = s[134:187]
print(address)
print(invoice)
print(redeem_code)

Thx for help.

2
  • What do you want to get from` {"address": "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK", "invoice": "invNrKU2ZFMuAJKUiejyVe3X34ybP9awyWZBfUEdY2dZKxYTB8ajW", "redeem_code": "BTCvQDD9xFYHHDYNi1JYeLY1eEkGFBFB49qojETjLBZ2CVYyPm56B"}` Commented Nov 4, 2017 at 19:25
  • 4
    this is json data, don't use regex to extract those. Use json.loads() and you'll get a python dictionary. Commented Nov 4, 2017 at 19:25

3 Answers 3

1

don't ever use regexes to parse structured data like this. Once parsed with proper means (json.loads or ast.literal_eval both work here), they become native python structure, trivial to access to.

In your case, using json.loads in one line:

import json

print(json.loads('{"address": "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK", "balance": 1234, "pending_balance": 0, "paid_out": 0}')["balance"])

result:

1234

(same method applies for your second question)

Sign up to request clarification or add additional context in comments.

Comments

0

Actually what you are showing us is what in Python is called a dictionary. That is a set of key and values.

Look here for more info: https://docs.python.org/3.6/tutorial/datastructures.html#dictionaries

Your dictionary has the following keys and values:

"address" --> "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK"
"balance" --> 1234
"pending_balance" --> 0
"paid_out" --> 0

Now if what you have is a dictionary:

d = {"address": "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK", "balance": 1234, "pending_balance": 0, "paid_out": 0}

print(d.get('balanace')) #1234

If however what you have is an external file with that information or you got it from a web service of some sort, you have a string representation of a dictionary. Here is where the JSON-library becomes valuable:

import json

# Assuming you got a string
s = '{"address": "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK", "balance": 1234, "pending_balance": 0, "paid_out": 0}'
d = json.loads(s) # <-- converts the string to a dictionary

print(d.get('balance')) #1234

Comments

0

Your data looks like json, so the preferable way of dealing with it is parsing using json module

import json
parsed_data = json.loads(data)
balance = parsed_data['balance']

If using regular expressions is a must, you can use following code

import re
match = re.search('"balance": (\d+)', data)
balance = int(match.group(1))

In this example me use \d+ to match string of digits and parenthesis to create a group. Group 0 would be the whole matched string and group 1 - the first group we created.

Comments

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.