{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "################################################################\n", "## ImportingData #2.2\n", "## Atul Singh\n", "## www.datagenx.net\n", "################################################################" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# import\n", "import json" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reading json data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "with open(\"glossary.json\") as json_file:\n", " jdata = json.load(json_file)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "glossary : {'title': 'example glossary', 'GlossDiv': {'title': 'S', 'GlossList': {'GlossEntry': {'GlossTerm': 'Standard Generalized Markup Language', 'SortAs': 'SGML', 'Abbrev': 'ISO 8879:1986', 'GlossDef': {'GlossSeeAlso': ['GML', 'XML'], 'para': 'A meta-markup language, used to create markup languages such as DocBook.'}, 'Acronym': 'SGML', 'ID': 'SGML', 'GlossSee': 'markup'}}}}\n" ] } ], "source": [ "# reading key value from json file\n", "for key in jdata.keys():\n", " print(key,\" : \", jdata[key])" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "widget : {'debug': 'on', 'image': {'alignment': 'center', 'src': 'Images/Sun.png', 'name': 'sun1', 'hOffset': 250, 'vOffset': 250}, 'window': {'title': 'Sample Konfabulator Widget', 'height': 500, 'name': 'main_window', 'width': 500}, 'text': {'name': 'text1', 'hOffset': 250, 'data': 'Click Here', 'size': 36, 'alignment': 'center', 'onMouseUp': 'sun1.opacity = (sun1.opacity / 100) * 90;', 'vOffset': 100, 'style': 'bold'}}\n" ] } ], "source": [ "## Reading widget.json file\n", "fh = open(\"widget.json\")\n", "data = json.load(fh)\n", "for k in data.keys():\n", " print(k,\" : \", data[k] )" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "

Pizza is a yeasted flatbread generally topped with tomato sauce and cheese and baked in an oven. It is commonly topped with a selection of meats, vegetables and condiments. The term was first recorded in the 10th century, in a Latin manuscript from Gaeta in Central Italy. The modern pizza was invented in Naples, Italy, and the dish and its variants have since become popular and common in many areas of the world.

\n", "

In 2009, upon Italy's request, Neapolitan pizza was safeguarded in the European Union as a Traditional Speciality Guaranteed dish. The Associazione Verace Pizza Napoletana (the True Neapolitan Pizza Association) is a non-profit organization founded in 1984 with headquarters in Naples. It promotes and protects the \"true Neapolitan pizza\".

\n", "

Pizza is sold fresh or frozen, either whole or in portions, and is a common fast food item in Europe and North America. Various types of ovens are used to cook them and many varieties exist. Several similar dishes are prepared from ingredients commonly used in pizza preparation, such as calzone and stromboli.

\n", "

\n" ] } ], "source": [ "# Import package\n", "import requests\n", "\n", "# Assign URL to variable: url\n", "url = 'https://en.wikipedia.org/w/api.php?action=query&prop=extracts&format=json&exintro=&titles=pizza'\n", "\n", "# Package the request, send the request and catch the response: r\n", "r = requests.get(url)\n", "\n", "# Decode the JSON data into a dictionary: json_data\n", "json_data = r.json()\n", "\n", "# Print the Wikipedia page extract\n", "pizza_extract = json_data['query']['pages']['24768']['extract']\n", "print(pizza_extract)" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "{\"Title\":\"The Social Network\",\"Year\":\"2010\",\"Rated\":\"PG-13\",\"Released\":\"01 Oct 2010\",\"Runtime\":\"120 min\",\"Genre\":\"Biography, Drama\",\"Director\":\"David Fincher\",\"Writer\":\"Aaron Sorkin (screenplay), Ben Mezrich (book)\",\"Actors\":\"Jesse Eisenberg, Rooney Mara, Bryan Barter, Dustin Fitzsimons\",\"Plot\":\"Harvard student Mark Zuckerberg creates the social networking site that would become known as Facebook, but is later sued by two brothers who claimed he stole their idea, and the co-founder who was later squeezed out of the business.\",\"Language\":\"English, French\",\"Country\":\"USA\",\"Awards\":\"Won 3 Oscars. Another 161 wins & 162 nominations.\",\"Poster\":\"https://images-na.ssl-images-amazon.com/images/M/MV5BMTM2ODk0NDAwMF5BMl5BanBnXkFtZTcwNTM1MDc2Mw@@._V1_SX300.jpg\",\"Metascore\":\"95\",\"imdbRating\":\"7.7\",\"imdbVotes\":\"491,784\",\"imdbID\":\"tt1285016\",\"Type\":\"movie\",\"Response\":\"True\"}\n" ] } ], "source": [ "# Import requests package\n", "import requests\n", "\n", "# Assign URL to variable: url\n", "url = 'http://www.omdbapi.com/?t=social+network'\n", "\n", "# Package the request, send the request and catch the response: r\n", "r = requests.get(url)\n", "\n", "# Print the text of the response\n", "print(r.text)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "############################################################\n", "## Atul Singh | www.datagenx.net | lnked.in/atulsingh\n", "############################################################" ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python [conda root]", "language": "python", "name": "conda-root-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.2" } }, "nbformat": 4, "nbformat_minor": 1 }