简体   繁体   English

Python使用正则表达式在字符串foo:之后提取数字

[英]Python Extract numbers in after the string foo: with regex

I have data like this: {"address": "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK", "balance": 1234, "pending_balance": 0, "paid_out": 0} 我有这样的数据: {"address": "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK", "balance": 1234, "pending_balance": 0, "paid_out": 0}

I want extract numbers after balance, but its can be from 0 to infinity. 我想在平衡后提取数字,但是它可以从0到无穷大。

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: 我已经得到的数据是这样{"address": "1GocfVCWTiRViPtqZetcX4UiCxnKxgTHwK", "invoice": "invNrKU2ZFMuAJKUiejyVe3X34ybP9awyWZBfUEdY2dZKxYTB8ajW", "redeem_code": "BTCvQDD9xFYHHDYNi1JYeLY1eEkGFBFB49qojETjLBZ2CVYyPm56B"}请告诉我这样做的正常方式:

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. 谢谢。

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. 一旦使用适当的方法进行解析( json.loadsast.literal_eval都可以在此处工作),它们就变成了原生python结构,访问起来很简单。

In your case, using json.loads in one line: 在您的情况下,在一行中使用json.loads

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) (同样的方法适用于您的第二个问题)

Actually what you are showing us is what in Python is called a dictionary. 实际上,您向我们展示的是在Python中称为字典的东西。 That is a set of key and values. 那是一组键和值。

Look here for more info: https://docs.python.org/3.6/tutorial/datastructures.html#dictionaries 在此处查找更多信息: 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. 但是,如果您拥有的是带有该信息的外部文件,或者您是从某种Web服务获取的,则您具有字典的字符串表示形式。 Here is where the JSON-library becomes valuable: JSON库在这里变得很有价值:

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

Your data looks like json, so the preferable way of dealing with it is parsing using json module 您的数据看起来像json,因此处理数据的更好方法是使用json模块进行解析

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. 在此示例中,我使用\\ d +匹配数字字符串和括号以创建组。 Group 0 would be the whole matched string and group 1 - the first group we created. 组0是整个匹配的字符串,组1是我们创建的第一个组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM