Python打印json字符串中的regex

2024-05-16 23:46:05 发布

您现在位置:Python中文网/ 问答频道 /正文

所以,我使用了一个小python脚本来简单地打印出一个网站的每一次西北大厅.,其中通配符()是一个数字,位于从url中提取的非常大的json字符串中。在

到目前为止,我有这个: 导入urllib,json,re

url = 'http://graphite.website.com/render/?target=stats.web.northwest.hall.*&format=json'
response = urllib.urlopen(url)
data = json.loads(response.read())
code = re.findall('northwest', data)
print code

这应该返回30个正则表达式的列表西北大厅号在正在解析的json字符串中,但我得到了以下错误:

^{pr2}$

Python新手(当然你能看出来)。 提前谢谢。在


Tags: 字符串re脚本jsonhttpurldata网站
1条回答
网友
1楼 · 发布于 2024-05-16 23:46:05

使用

data = response.read()

从服务器获取json字符串。在

使用

^{pr2}$

将这个字符串转换成python字典。在


编辑:

import re

data = """
stats.web.northwest.hall.01
stats.web.northwest.hall.223
stats.web.northwest.hall.31
stats.web.northwest.hall.4
"""

print re.findall(r'stats.web.northwest.hall.(\d+)', data)

['01', '223', '31', '4']

相关问题 更多 >