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

DSA in C# | Data Structure and Algorithm using C#

  DSA in C# |  Data Structure and Algorithm using C#: Lecture 1: Introduction to Data Structures and Algorithms (1 Hour) 1.1 What are Data Structures? Data Structures are ways to store and organize data so it can be used efficiently. Think of data structures as containers that hold data in a specific format. Types of Data Structures: Primitive Data Structures : These are basic structures built into the language. Example: int , float , char , bool in C#. Example : csharp int age = 25;  // 'age' stores an integer value. bool isStudent = true;  // 'isStudent' stores a boolean value. Non-Primitive Data Structures : These are more complex and are built using primitive types. They are divided into: Linear : Arrays, Lists, Queues, Stacks (data is arranged in a sequence). Non-Linear : Trees, Graphs (data is connected in more complex ways). Example : // Array is a simple linear data structure int[] number...

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...

Conditional Statement in Python

It is used to solve condition-based problems using if and else block-level statement. it provides a separate block for  if statement, else statement, and elif statement . elif statement is similar to elseif statement of C, C++ and Java languages. Type of Conditional Statement:- 1) Simple if:- We can write a single if statement also in python, it will execute when the condition is true. for example, One real-world problem is here?? we want to display the salary of employees when the salary will be above 10000 otherwise not displayed. Syntax:- if(condition):    statements The solution to the above problem sal = int(input("Enter salary")) if sal>10000:     print("Salary is "+str(sal)) Q)  WAP to increase the salary of employees from 500 if entered salary will be less than 10000 otherwise the same salaries will be displayed. Solution:- x = int(input("enter salary")) if x<10000:     x=x+500 print(x)   Q) WAP to display th...