How do I get the data extracted from API to my database

Submitted 7 months, 2 weeks ago
Ticket #440
Views 139
Language Python
Priority Low
Status Closed

I was able to use an api to get the data, for example:

{"aid": "1773022", "url": "hdsrgrg022", "papers": [{"paperId": "17", "title": "fdgfdg"}, {"paperId": "71f49f1e3ccb2e92d606db9b3db66c669a163bb6", "title": "Task-Driven Learning fdgfdgal Features"}, {"paperId": "bb35", "title": "Sfdgfdgchies"}]}

How would I use python to turn this into a table so I can use it to build my database?

Submitted on Feb 16, 22
add a comment

1 Answer

Verified

Step1 :

we have to connect to the source API using below logic as a example,

import json
 
url = "https://api.exchangeratesapi.io/latest?symbols=USD,GBP"
 
response = requests.get(url)
data = response.text
parsed = json.loads(data)
date = parsed["date"]
 
gbp_rate = parsed["rates"]["GBP"]
usd_rate = parsed["rates"]["USD"]

Step 2:

As soon as we have cleansed/transforming the data , we need to just load into target database and respective tables.

import csv
import MySQLdb

mydb = MySQLdb.connect(host='localhost',
    user='root',
    passwd='',
    db='mydb')
cursor = mydb.cursor()

csv_data = 
for row in data:

    cursor.execute('INSERT INTO testcsv(names, \
          classes, mark )' \
          'VALUES("%s", "%s", "%s")', 
          row)
#close the connection to the database.
mydb.commit()
cursor.close()
print "Done"

Submitted 7 months, 2 weeks ago


Wanna Post Ad?
Reach a large group of audience by posting an ad about your business here. For more details
Drop us a Message