Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #68008
| From | Kev Dwyer <kevin.p.dwyer@gmail.com> |
|---|---|
| Subject | Re: extract from json |
| Date | 2014-03-07 21:05 +0000 |
| References | <d72d9e78-ee11-4833-b3b2-e90ab52a6d4c@googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.7908.1394226337.18130.python-list@python.org> (permalink) |
teddybubu@gmail.com wrote:
> I can't find any example on how to do this.
> I have a json file like so:
> {"bostock":
[{"url":"http://bl.ocks.org/mbostock/9360565","title":"titleplaceholder","date":"dateplaceholder"},
>
{"url":"http://bl.ocks.org/mbostock/9265674","title":"titleplaceholder","date":"dateplaceholder"},
>
{"url":"http://bl.ocks.org/mbostock/9265467","title":"titleplaceholder","date":"dateplaceholder"},
>
{"url":"http://bl.ocks.org/mbostock/9234731","title":"titleplaceholder","date":"dateplaceholder"},
>
{"url":"http://bl.ocks.org/mbostock/9232962","title":"titleplaceholder","date":"dateplaceholder"},
>
> this goes on for more than 700 entries. only thing unique is the number at
> the end of the url. I am going to load the url in python, get the date and
> title and write it in the json itself. Right now I am stuck on just
> reading the url in the json. Here is my code:
>
> import json
>
> with open("bostock.json") as json_file:
> json_data = json.load(json_file)
> print(json_data)
>
> I have tried json_data[0], json_data.url and a few others I forget right
> now and it does not seem to work.
>
> I have already figured out how to get the title and date.
> First things first: How can i just get the url for each line of the above
> json file?
Hello
Try:
Python 2.7.2 (default, Aug 19 2011, 20:41:43) [GCC] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> with open('/tmp/bostock.json') as f:
... json_data = json.load(f)
...
>>> json_data
{u'bostock': [{u'url': u'http://bl.ocks.org/mbostock/9360565', u'date':
u'dateplaceholder', u'title': u'titleplaceholder'}, {u'url':
u'http://bl.ocks.org/mbostock/9265674', u'date': u'dateplaceholder',
u'title': u'titleplaceholder'}, {u'url':
u'http://bl.ocks.org/mbostock/9265467', u'date': u'dateplaceholder',
u'title': u'titleplaceholder'}, {u'url':
u'http://bl.ocks.org/mbostock/9234731', u'date': u'dateplaceholder',
u'title': u'titleplaceholder'}, {u'url':
u'http://bl.ocks.org/mbostock/9232962', u'date': u'dateplaceholder',
u'title': u'titleplaceholder'}]}
>>> urls = [x['url'] for x in json_data['bostock']]
>>> urls
[u'http://bl.ocks.org/mbostock/9360565',
u'http://bl.ocks.org/mbostock/9265674',
u'http://bl.ocks.org/mbostock/9265467',
u'http://bl.ocks.org/mbostock/9234731',
u'http://bl.ocks.org/mbostock/9232962']
Python loads the json in the file into a dictionary. In this case, the
dictionary has a single key, 'bostock', and the value in the dictionary for
that key is a list (of dictionaries).
To get the urls, you need to get the list
json_data['bostock']
and then iterate over it's elements, getting the value for the key url for
each one.
This is what the list comprehension
[x['url'] for x in json_data['bostock']]
does.
I hope that helps,
Kev
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
extract from json teddybubu@gmail.com - 2014-03-07 12:27 -0800
Re: extract from json Kev Dwyer <kevin.p.dwyer@gmail.com> - 2014-03-07 21:05 +0000
Re: extract from json teddybubu@gmail.com - 2014-03-07 19:02 -0800
Re: extract from json thrinaxodon666@gmail.com - 2014-03-07 19:21 -0800
Re: extract from json Chris Angelico <rosuav@gmail.com> - 2014-03-08 14:49 +1100
spam (wasRe: extract from json) Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-03-08 13:13 +0000
Re: spam (wasRe: extract from json) Chris Angelico <rosuav@gmail.com> - 2014-03-09 00:19 +1100
csiph-web