【发布时间】:2014-02-26 05:19:39
【问题描述】:
我是 python 新手,正在尝试研究如何将一些 json 插入 mysql。我正在运行一个我已经拥有的输出 json 的 shell 脚本。
balance = subprocess.check_output([shell_script, 'all'])
conn = MySQLdb.connect(all my info)
cursor = conn.cursor()
#ive tried a few different things but this is the last one i tried
for i in balance.items():
term = i[0]
urls = json.dumps(i[1])
sql = """INSERT INTO balance (name, balance) VALUES (%s, %s)"""
cursor.execute(sql, (term, urls))
但我尝试的一切都得到了
AttributeError: 'str' object has no attribute 'items'
这是 shell 脚本的输出样子
{
"" : 52,
"bob" : 12,
"john" : 2,
"peter" : 4
}
【问题讨论】: