0

So, I'm using a small python script to try to simply print out every occurrence of a website 'northwest.hall.' where the wildcard() is a number, in a very large json string pulled from a url.

I have this so far: import 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

This should return a list of the 30 regexpressions of northwest.hall.number in the json string being parsed, but I get the following error instead:

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

New to Python (sure you can tell). Thanks in advance.

3
  • 1
    Have you verified that the data you are pulling down looks like what you think it does before applying the regex? Commented Jun 19, 2014 at 23:08
  • 1
    When I try your URL, I get 404 - File or directory not found. Commented Jun 19, 2014 at 23:11
  • I get 404 too so I use httpbin.org/headers to get some json from server httpbin.org Commented Jun 19, 2014 at 23:15

1 Answer 1

1

Use

data = response.read()

to get json string from server.

Using

data = json.loads(response.read())

you change this string into python dictionary.


EDIT:

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']
Sign up to request clarification or add additional context in comments.

4 Comments

Awesome furas, always humbling hearing from a pro. I get back 30 instances of 'northwest'. How do I get just the numbers following the string stats.web.northwest.hall. ? Thanks again!
Do you mean something like example in my answer ?
Perfect! If you're ever in San Francisco, I want to buy you a beer. In fact, many.
San Francisco is too far for me :( I'm from Poland in Europe ;)

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.