-1

谁能帮我弄清楚我在做什么愚蠢?我正在尝试使用 json 文件中的财务数据传播 SQL 表。我在标题中得到错误,但我似乎无法弄清楚它来自哪里。

import json
import MySQLdb

open_time     = 0
openp         = 0
high          = 0
low           = 0
closep        = 0
volume        = 0
close_time    = 0
quoteassetvol = 0
trades        = 0
ignore1       = 0
ignore2       = 0
ignore3       = 0

con = MySQLdb.connect(host='localhost',user='root',db='binance_adabtc',passwd='abcde')
cur = con.cursor()

symbols = ["(JSON_EXTRACT('json_obj','$[i][open_time]'), (JSON_EXTRACT('json_obj','$[i][openp]'), (JSON_EXTRACT('json_obj','$[i][high]'), (JSON_EXTRACT('json_obj','$[i][low]'), (JSON_EXTRACT('json_obj','$[i][closep]'),(JSON_EXTRACT('json_obj','$[i][volume]'), (JSON_EXTRACT('json_obj','$[i][close_time]'), (JSON_EXTRACT('json_obj','$[i][quoteassetvol]'), (JSON_EXTRACT('json_obj','$[i][trades]'),(JSON_EXTRACT('json_obj','$[i][ignore1]'), (JSON_EXTRACT('json_obj','$[i][ignore2]'), (JSON_EXTRACT('json_obj','$[i][ignore3]'))"]

json_file = open("C:\Users\Mike\Desktop\Binance\Binance_ADABTC_5m_1512086400000-1529971200000.json","r")
json_obj = json.load(json_file)
json_file.close()

column_str = "(open_time, openp, high, low, closep, volume, close_time, quoteassetvol, trades, ignore1, ignore2, ignore3)"
insert_str = ("%s, "*12)[:-2]
final_str  = "INSERT INTO Binance_ADABTC_5m_1512086400000_1529971200000 (%s) VALUES (%s)" % (column_str, insert_str)

for i in range (0,178848):
    cur.execute(final_str,symbols)

con.commit()
#cnx.commit()
con.close()

文件中的数据如下所示:

[[1512086400000, "0.00001204", "0.00001209", "0.00001161", "0.00001183", "771721.00000000", 1512086699999, "9.10638040", 126, "359700.00000000", "4.22792312", "52516956.22676400"], [1512086700000, "0.00001189 “,” 0.00001194“,“ 0.00001183”,“ 0.00001189”,“ 119120.00000000”,151208699999999,“ 1.41575664”,44,44,“ 49016.00000000”,“ 49016.00000000 “ 0.00001183”,“ 0.00001190”,“ 260885.00000000”,1512087299999,“ 3.10077566”,65,“ 152594.000000000000000000000000000000000000000000000000000000000000000000个”,“ 1.8177862”,“ 1.8177862”,“00001196“,“ 0.00001181”,“ 0.00001184”,“ 508088.00000000”,15120875999999,“ 6.0301010107”,95,95,“ 123506.00000000000000”,“ 1.46831546”,“ ,“ 0.00001174”,“ 312477.00000000”,1512087899999,“ 3.69618651”,63,“ 155121.000000”,“ 1.84118817”,“ 1.84118817”,“ 53289721.44428790011.000110001000010000100001000010000100001000010000100001000000010000、151000、151000、151000、151000、151000、151000” 296718.00000000”、1512088199999、“3.43350676”、66、“148......46831546", "52859721.84287900"], [1512087600000, "0.00001187", "0.00001190", "0.00001171", "0.00001174", "312477.00000000", 1512087899999, "3.69618651", 63, "155121.00000000", "1.84118817", "53289721.44287900" ]、[1512087900000、“0.00001170”、“0.00001177”、“0.00001155”、“0.00001156”、“296718.00000000”、1512088199999、“3.43350676”、66、“148......46831546", "52859721.84287900"], [1512087600000, "0.00001187", "0.00001190", "0.00001171", "0.00001174", "312477.00000000", 1512087899999, "3.69618651", 63, "155121.00000000", "1.84118817", "53289721.44287900" ]、[1512087900000、“0.00001170”、“0.00001177”、“0.00001155”、“0.00001156”、“296718.00000000”、1512088199999、“3.43350676”、66、“148......00001170”、“0.00001177”、“0.00001155”、“0.00001156”、“296718.00000000”、1512088199999、“3.43350676”、66、“148......00001170”、“0.00001177”、“0.00001155”、“0.00001156”、“296718.00000000”、1512088199999、“3.43350676”、66、“148......

The full stack trace is:
    Traceback (most recent call last):
  File "C:/Users/Mike/.PyCharmCE2018.1/config/scratches/scratch_6.py", line 27, in <module>
    cur.execute(final_str,symbols)
  File "C:\Python27\lib\site-packages\MySQLdb\cursors.py", line 187, in execute
    query = query % tuple([db.literal(item) for item in args])
TypeError: not enough arguments for format string
4

1 回答 1

1

我可能在这里遗漏了一些东西,我不会放弃使用符号来解决这个问题。如果您只是想从 json 行中提取数据并将它们放在数据库中受尊重的列中,那么它应该会简单得多。也许是这样的。

import json
import MySQLdb

con = MySQLdb.connect(host='localhost',user='root',db='binance_adabtc',passwd='abcde')
cur = con.cursor()

json_file = open("C:\Users\Mike\Desktop\Binance\Binance_ADABTC_5m_1512086400000-1529971200000.json","r")
json_obj = json.load(json_file)
json_file.close()

column_str = "(open_time, openp, high, low, closep, volume, close_time, quoteassetvol, trades, ignore1, ignore2, ignore3)"
insert_str = ("%s, "*12)[:-2]
final_str = """INSERT INTO Binance_ADABTC_5m_1512086400000_1529971200000 (%s) VALUES (%s)""" % (column_str, insert_str)

for i in json_obj:
    #print final_str % tuple(i)
    cur.execute(final_str,tuple(i))

con.commit()
cnx.commit()
con.close()

另一种稍微不同的pythonic方法。如果您在数据库列格式化方面遇到问题,请尝试删除您的表并运行它。它也有创建表。

import json
import MySQLdb

#Open db and create cursor
con = MySQLdb.connect(host='localhost',
                      user='root',
                      db='binance_adabtc',
                      passwd='abcde')
cur = con.cursor()

#Filepath
filePath = """C:\Users\Mike\Desktop\Binance\Binance_ADABTC_5m_1512086400000-1529971200000.json"""

#Open file and put it in list
json_file = open(filePath,"r")
json_list = json.load(json_file)
json_file.close()

#Split filepath the extract name
table_name = filePath.split("\\")[-1].split(".")[0].replace("-", "_")

#MySQL create table statement
cur.execute("""CREATE TABLE IF NOT EXISTS {} (id INT PRIMARY KEY AUTO_INCREMENT, \
                                  open_time BIGINT, \
                                  openp FLOAT, \
                                  high FLOAT, \
                                  low FLOAT, \
                                  closep FLOAT, \
                                  volume FLOAT, \
                                  close_time BIGINT, \
                                  quoteassetvol FLOAT, \
                                  trades INT, \
                                  ignore1 FLOAT, \
                                  ignore2 FLOAT, \
                                  ignore3 FLOAT)""".format(table_name))
con.commit()

#MySQL create table and insert statement
insert = """INSERT INTO {} (open_time, \
                            openp, \
                            high, \
                            low, \
                            closep, \
                            volume, \
                            close_time, \
                            quoteassetvol, \
                            trades, \
                            ignore1, \
                            ignore2, \
                            ignore3) \
                            VALUES ({})""".format(table_name, ("%s, "*12)[:-2])
#Print the insert if needed
#print " ".join(insert.split())

#Loop through list and insert
for i in json_list:
    cur.execute(insert, tuple(i))

#Commit and close
con.commit()
con.close()

请注意, cursor.execute() 需要元组而不是多个输入的列表。

于 2018-06-29T02:03:09.957 回答