json字符串中的Python打印正则表达式

json字符串中的Python打印正则表达式,python,regex,json,Python,Regex,Json,因此,我使用一个小python脚本尝试简单地打印出网站“northwest.hall.”的每一次出现,其中通配符()是一个数字,在一个从url提取的非常大的json字符串中 到目前为止,我有: 导入urllib、json、re url = 'http://graphite.website.com/render/?target=stats.web.northwest.hall.*&format=json' response = urllib.urlopen(url) data = json

因此,我使用一个小python脚本尝试简单地打印出网站“northwest.hall.”的每一次出现,其中通配符()是一个数字,在一个从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
这将返回正在解析的json字符串中northwest.hall.number的30个正则表达式的列表,但我得到了以下错误:

Traceback (most recent call last):
  File "/Users/arin/Desktop/scripts/code_parser2.py", line 7, in <module>
    code = re.findall('community', data)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py", line 177, in findall
    return _compile(pattern, flags).findall(string)
TypeError: expected string or buffer
回溯(最近一次呼叫最后一次):
文件“/Users/arin/Desktop/scripts/code_parser2.py”,第7行,在
代码=re.findall('社区',数据)
findall中的文件“/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/re.py”,第177行
返回编译(模式、标志).findall(字符串)
TypeError:应为字符串或缓冲区
Python新手(当然你知道)。 提前感谢。

使用

data = response.read()
从服务器获取json字符串

使用

data = json.loads(response.read())
将此字符串更改为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']

你是否验证了你正在提取的数据与你在应用正则表达式之前所想的一样?当我尝试你的URL时,我得到了
404-找不到文件或目录
。我也得到了
404
,所以我经常从服务器Awesome furas获得一些json,总是让专业人士感到羞愧。我找到了30个“西北”的例子。如何仅获取字符串stats.web.northwest.hall后面的数字?再次感谢!你的意思是像我回答中的例子吗?太好了!如果你在旧金山,我想给你买杯啤酒。事实上,很多。旧金山对我来说太远了(我来自欧洲的波兰);