Skip to main content

How to fetch data from remote url using JSON format and convert into numpy array?


Python provides JSON Module to read JSON data and urlib.request module to fetch data from remote URL?

This is the example to fetch records from JSON File.

Solution:-

import json

import urllib.request

import numpy as np

data =  urllib.request.urlopen("https://shivaconceptsolution.com/webservices/showdriver.php");

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

#print(s)

arr1 = []

arr2 = []

for k in s['result']:

    arr1.append(k['id'])

    arr2.append(k['uname'])

      # print(k['id'],k['uname'])

    print(arr1, arr2) 

a = np.array(arr1)

b = np.array(arr2)

print(a,b)

Assignment:-

1) USE the SAME API to find max id, min id, avg id, repeated mobile number, null record rows, max null record rows.

2) Use Covid LIVE API and Provide Current Status, Country-wise, State-wise?



Comments

  1. # To Find Largest Element in the List
    data = urllib.request.urlopen("https://shivaconceptsolution.com/webservices/showdriver.php");

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

    arr1 = np.array([],dtype=int)

    def Large(list1):
    m = list1[0]
    for x in list1:
    if x > m :
    m = x
    return m

    for d in s["result"]:

    arr3=np.append(arr1,d["id"])

    print(arr3)

    print("Largest element is:", Large(arr3))

    ReplyDelete
  2. # Data Science ( 6 To 7 Pm BATCH)
    # Finding Max Id, Avg Id, Repeated Mobile Number.

    import json

    import urllib.request

    import numpy as np



    data = urllib.request.urlopen("https://shivaconceptsolution.com/webservices/showdriver.php");

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

    idd= 0

    b = 0

    av = 0

    lst = []

    for d in s["result"]:

    b+=1
    arr3=np.append(arr1,d["id"])

    print("ID. :-",arr3)

    arr4=np.append(arr2,d["uname"])

    print("User Name:-",arr4)

    arr5=np.append(arr2,d['emailid'])

    print("Email.ID:-",arr5)

    arr6=np.append(arr2,d['mobile'])

    print("Mobile No. :-",arr6,"\n")

    av = av+int(d["id"])

    lst.append(d['mobile'])


    if int(d["id"])>idd:
    idd = int(d["id"])

    print("Maximum ID :-",idd,"\n")
    print("Average ID :-",av/b)

    for d in s["result"]:
    mo = lst.count(d['mobile'])
    if mo>1:
    print(d,"Repeated :-",mo,"Times")





    ReplyDelete
  3. :- code for find repeted numbers in list
    #Jayant Chawliya

    lst=['12345','4567','12345','4567']
    s= set(lst)

    for d in s:
    c=lst.count(d)
    if c>1:
    print(d,"Repeted :-",c,"Times")

    ReplyDelete
  4. # Data Science ( 6 To 7 Pm Batch)

    # Code to find max id, avg id, repeated mobile number from URL.

    import json

    import urllib.request

    import numpy as np



    data = urllib.request.urlopen("https://shivaconceptsolution.com/webservices/showdriver.php");

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

    idd= 0

    b = 0

    av = 0

    lst = []

    arr1 = np.array([],dtype=int)

    arr2 = np.array([],dtype=str)


    for d in s["result"]:

    b+=1
    arr3=np.append(arr1,d["id"])

    print("ID. :-",arr3)

    arr4=np.append(arr2,d["uname"])

    print("User Name:-",arr4)

    arr5=np.append(arr2,d['emailid'])

    print("Email.ID:-",arr5)

    arr6=np.append(arr2,d['mobile'])

    print("Mobile No. :-",arr6,"\n")

    av = av+int(d["id"])

    lst.append(d['mobile'])# All mobile no. Append in lst[]


    if int(d["id"])>idd:
    idd = int(d["id"])

    print("Maximum ID :-",idd,"\n")
    print("Average ID :-",av/b)


    # Code to Find Repeated Mobile No.
    lst1 = [] # For unique Mobile No.
    for i in lst:
    if i not in lst1:
    lst1.append(i)


    for d in s["result"]:
    mo = lst1.count(d['mobile'])
    if mo>1:
    print(d,"Repeated :-",mo,"Times")





    ReplyDelete
  5. # Excel Sheet "Record"
    #worksheet="A"
    #worksheet="B"
    #Combine Both A and B into python using openpyxl
    import openpyxl

    l1 = 0
    l2 = 1
    wb = openpyxl.load_workbook('Record.xlsx')
    sheets = wb.sheetnames
    ws = wb[sheets[l1]]
    print(ws)
    ws1 = wb[sheets[l2]]
    print(ws1)

    print("\n\nSheet 1 Data \n")
    ar =[]#Empty List
    col = ws.max_column
    ro = ws.max_row
    for i in range(1,(ro+1)):
    k=0
    ar1=[]#Empty List
    for j in range(1,(col+1)):
    c1 = ws.cell(row = i, column = j)
    ar1.append(c1.value)
    k+=1
    ar.append(ar1)
    arr = np.concatenate((ar))
    print(arr)
    print("\n")

    print("Sheet 2 Data \n")

    a =[]#Empty List
    col1 = ws1.max_column
    ro1 = ws1.max_row

    for k in range(1,(ro1+1)):
    k1=0
    ar2=[]#Empty List
    for p in range(1,(col1+1)):
    c12 = ws1.cell(row = k, column = p)
    ar2.append(c12.value)
    k1+=1
    a.append(ar2)
    arrr = np.concatenate((a))
    print(arrr)

    ReplyDelete
  6. import json
    import urllib.request
    import numpy as np
    data = urllib.request.urlopen("https://shivaconceptsolution.com/webservices/showdriver.php");
    s= json.loads(data.read())
    l=[]
    l1=[]
    l2=[]
    l3=[]
    l4=[]
    for i in s["result"]:
    for j in i:

    l4.append(i[j])
    l.append(int(i.get("id")))
    l1.append(i.get("uname"))
    l2.append(i.get("emailid"))
    l3.append(i.get("mobile"))
    print("max id:",max(l))
    print("min id:",min(l))
    print("average id:",sum(l)/len(l))
    for i in l3:
    b=l3.count(l3)
    l8=[]
    print("repeated mobile no.",b)
    co=0
    m=0
    for i in range(0,len(l4),9):
    c=l4[0+i:9+i]
    d=c.count("")
    l8.append(d)
    co=co+1
    print(co,"row null value:",d)
    for i in range(0,len(l8)):
    if l8[i]>m:
    lk+=1
    m=l8[i]
    lk=i+1
    print("repeated mobile mobile no:",b)
    print("max null record rows",lk)

    ReplyDelete
  7. import json
    j = '[{"a":"1", "b":"2", "c":"3", "d":"4"}]'
    a = json.loads(j)
    for i in range(len(a)):
    for j in a[i]:
    print(j,":",a[i][j])

    ReplyDelete
  8. import json
    f = open('P:\\excel\\pk.json')
    a = json.load(f)
    for i in a['result']:
    for j in i:
    print(j,":",i[j])

    ReplyDelete
  9. import csv

    f= open("P:\\word\\covid_19_india.csv")
    c =csv.reader(f)
    c1 = 0
    for row in c:
    print(row)
    c1+=1
    print("line count",c1)

    ReplyDelete

Post a Comment

POST Answer of Questions and ASK to Doubt

Popular posts from this blog

Uncontrolled form input in React-JS

  Uncontrolled form input in React-JS? If we want to take input from users without any separate event handling then we can uncontrolled the data binding technique. The uncontrolled input is similar to the traditional HTML form inputs. The DOM itself handles the form data. Here, the HTML elements maintain their own state that will be updated when the input value changes. To write an uncontrolled component, you need to use a ref to get form values from the DOM. In other words, there is no need to write an event handler for every state update. You can use a ref to access the input field value of the form from the DOM. Example of Uncontrolled Form Input:- import React from "react" ; export class Info extends React . Component {     constructor ( props )     {         super ( props );         this . fun = this . fun . bind ( this ); //event method binding         this . input = React . createRef ();...

JSP Page design using Internal CSS

  JSP is used to design the user interface of an application, CSS is used to provide set of properties. Jsp provide proper page template to create user interface of dynamic web application. We can write CSS using three different ways 1)  inline CSS:-   we will write CSS tag under HTML elements <div style="width:200px; height:100px; background-color:green;"></div> 2)  Internal CSS:-  we will write CSS under <style> block. <style type="text/css"> #abc { width:200px;  height:100px;  background-color:green; } </style> <div id="abc"></div> 3) External CSS:-  we will write CSS to create a separate file and link it into HTML Web pages. create a separate file and named it style.css #abc { width:200px;  height:100px;  background-color:green; } go into Jsp page and link style.css <link href="style.css"  type="text/css" rel="stylesheet"   /> <div id="abc"> </div> Exam...

JDBC using JSP and Servlet

JDBC means Java Database Connectivity ,It is intermediates from Application to database. JDBC has different type of divers and provides to communicate from database server. JDBC contain four different type of approach to communicate with Database Type 1:- JDBC-ODBC Driver Type2:- JDBC Vendor specific Type3 :- JDBC Network Specific Type4:- JDBC Client-Server based Driver  or JAVA thin driver:- Mostly we prefer Type 4 type of Driver to communicate with database server. Step for JDBC:- 1  Create Database using MYSQL ,ORACLE ,MS-SQL or any other database 2   Create Table using database server 3   Create Form according to database table 4  Submit Form and get form data into servlet 5  write JDBC Code:-     5.1)   import package    import java.sql.*     5.2)  Add JDBC Driver according to database ide tools     5.3)  call driver in program         ...