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

9


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?



Post a Comment

9Comments

POST Answer of Questions and ASK to Doubt

  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