Python - 用于写入云数据库的格式API数据

时间:2015-03-12 07:37:23

标签: python mysql-python

我是Python的新手。我使用我在Github上的SDK中找到的API调用成功地从基于Web的数据库中提取数据。数据恢复正常。我需要对其进行格式化,以便将其写回AWS MySQL数据库。

第1步 - API查询:

这是我用来获取API数据的Python代码:

from infusionsoft.library import Infusionsoft
infusionsoft = Infusionsoft('APPNAME', 'API-SECRET-KEY')
table = 'Contact'
returnFields = ['Id', 'FirstName', 'LastName', 'Email', 'Leadsource']
query = {'FirstName' : 'John'}
limit = 5
page = 0
rawdata = infusionsoft.DataService('query', table, limit, page, query, returnFields)
print rawdata

返回一个如下所示的数据集:

[{' LastName':' Marden',' Leadsource':' Phillips',' Email': ' xxxxxxx@xxxxxxx.com',' FirstName':' John',' Id':602},{' LastName&# 39;:' Macfleckno',' Leadsource':' Old Leads',' Email':' xxxxxxx@xxxxxxx.com&# 39;,' FirstName':' John',' Id':722},{' LastName':' Donoghue&#39 ;,' Leadsource':' Phillips',' Email':' xxxxxxx@xxxxxxx.com',' FirstName' :' John',' Id':952},{' LastName':' Ostman',' Leadsource': ' Phillips','电子邮件' xxxxxxx@xxxxxxx.com',' FirstName':' John',& #39; Id':1046},{'姓氏':' Tassi',' Leadsource':'分类广告',&# 39;电子邮件':' xxxxxxx@xxxxxxx.com',' FirstName':' John',' Id':1070}]

第2步 - 写入数据库:

我有这个代码工作,它连接到我的数据库并将这个虚拟数据写入表:

import MySQLdb as mdb
con = mdb.connect(host = "HOSTNAME",user="USERNAME",passwd="PASSWORD", port=3306, db="DBNAME")
with con:
    cur = con.cursor()
    cur.execute("DROP TABLE IF EXISTS Writers")
    cur.execute("CREATE TABLE Writers(Id INT PRIMARY KEY AUTO_INCREMENT, \
             Name VARCHAR(25))")
    cur.execute("INSERT INTO Writers(Name) VALUES('Jack London')")
    cur.execute("INSERT INTO Writers(Name) VALUES('Honore de Balzac')")
    cur.execute("INSERT INTO Writers(Name) VALUES('Lion Feuchtwanger')")
    cur.execute("INSERT INTO Writers(Name) VALUES('Emile Zola')")
    cur.execute("INSERT INTO Writers(Name) VALUES('Truman Capote')")

这显然只是我在教程中找到的示例代码,但是与DB的连接正在运行,正在创建表并且正在编写数据。

那么,我如何遍历API调用返回的数据集并将其解析为适合写入数据库表的列和行?

谢谢

1 个答案:

答案 0 :(得分:0)

您从API返回的rawdata是一个带有五个dicts的JSON列表。 Python内置了对遍历JSON对象的支持,即你甚至不需要导入json。

python中的标准 for loop 允许您遍历列表。对于列表中的每个字典,您可以按名称访问所有键/值对(例如,writerObj [" FirstName"])。

from pprint import pprint
rawdata = [{'LastName': 'Marden', 'Leadsource': 'Phillips', 'Email': 'xxxxxxx@xxxxxxx.com', 'FirstName': 'John', 'Id': 602}, {'LastName': 'Macfleckno', 'Leadsource': 'Old Leads', 'Email': 'xxxxxxx@xxxxxxx.com', 'FirstName': 'John', 'Id': 722}, {'LastName': 'Donoghue', 'Leadsource': 'Phillips', 'Email': 'xxxxxxx@xxxxxxx.com', 'FirstName': 'John', 'Id': 952}, {'LastName': 'Ostman', 'Leadsource': 'Phillips', 'Email': 'xxxxxxx@xxxxxxx.com', 'FirstName': 'John', 'Id': 1046}, {'LastName': 'Tassi', 'Leadsource': 'Classifieds', 'Email': 'xxxxxxx@xxxxxxx.com', 'FirstName': 'John', 'Id': 1070}]
for writerObj in rawdata:
#    pprint(writerObj)
#    cur.execute("INSERT INTO Writers(Name) VALUES('" + writerObj["FirstName"] + ' ' + writerObj["LastName"] + "')")
    pprint("INSERT INTO Writers(Name) VALUES('" + writerObj["FirstName"] + ' ' + writerObj["LastName"] + "')")

pprint库只是将文本打印到屏幕上。注释掉的行" cur.execute(..."是您在代码中调用的与数据库接口的内容。