首页 > 解决方案 > 如何在 Python (array[25,65], array[25,42], array[25,24]) 中打印列表的值到 (25.65, 25.42, 25,24)

问题描述

我们正在开发一个 python 程序,我们无法将数据发送到我们的 MySQL 数据库。到目前为止,我们正在从我们的数据库中选择数据,我们想在我们的 python 程序中对数据做一些事情,然后将其发送回我们的数据库。

不幸的是,我们遇到了一些挑战,希望您能帮助我们。

我们收到此错误:

[SQL: INSERT INTO `Raw_Validated` (time_start, time_end, first_temp_lpn, first_temp_lpn_validated, second_temp_lpn, second_temp_lpn_validated, third_temp_lpn, third_temp_lpn_validated) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)]
[parameters: ('2019-08-29 16:20:00', '2019-08-29 17:20:00', array([25.69]), 1, array([25.21]), 1, array([25.09]), 1)]

我们可以得出结论,不是插入一个值,而是插入一个数组。我们不知道为什么会发生这种情况或如何防止这种情况发生,但我们希望它变成这样,而不是上面的参数:

[parameters: ('2019-08-29 16:20:00', '2019-08-29 17:20:00', 25.69, 1, 25.21, 1, 25.09, 1)]

我们正在运行一个迭代 3 次的 for 循环,这意味着我们正在接收 3 个 'a_temp' 值,这些值被保存到我们的列表 'list_lpn_temp 中(代码片段中没有显示 for 循环):

list_lpn_temp = []
new_list_lpn_temp = []

engine = create_engine("mysql://xxx:xxx@localhost/xxx")
conn = engine.connect()

a_temp = pd.read_sql('SELECT temperature FROM Raw_Data WHERE topic = "lpn1" AND timestamp > "%s" AND timestamp < "%s" ORDER BY timestamp DESC LIMIT 1' % (x, x+datetime.timedelta(minutes=20)), conn).astype(float).values

list_lpn_temp.extend(a_temp)

然后我们有另一个 for 循环(请记住,list_station 尚未初始化,但在我们的程序中已经初始化):

for i in range (len(list_lpn_temp)):
if -1.5 < list_station_temp[i]-list_lpn_temp[i] < 1.5:
    validated_lpn = 1
    list_validated.append(validated_lpn)
    new_list_lpn_temp.extend(list_lpn_temp[i]) 
    print(f'New LPN List = {new_list_lpn_temp}')
else:
    validated_lpn = 0
    list_validated.append(validated_lpn)

然后我们准备数据,以便我们可以将其进一步发送到数据库(这里有很多新的未初始化变量,我们已经在我们的程序中初始化了,但这里没有,因为它们根本不重要)。只有 list_lpn_temp[] 在这里很重要:

df2 = pd.DataFrame(columns=['time_start', 'time_end', 'first_temp_lpn', 'first_temp_lpn_validated', 'second_temp_lpn', 'second_temp_lpn_validated', 'third_temp_lpn', 'third_temp_lpn_validated'])
df2 = df2.append({'time_start' : time_start, 'time_end' : time_end, 'first_temp_lpn' : list_lpn_temp[0], 'first_temp_lpn_validated' : list_validated[0], 'second_temp_lpn' : list_lpn_temp[1], 'second_temp_lpn_validated' : list_validated[1$

with engine.connect() as conn, conn.begin():
    df2.to_sql('Raw_Validated', conn, if_exists='append', index=False)

标签: pythondataframesqlalchemy

解决方案


只需为所有访问添加一级索引list_lpn_temp,so list_lpn_temp[0]will become list_lpn_temp[0][0]and list_lpn_temp[1]will become list_lpn_temp[1][0]etc.

df2 = pd.DataFrame(columns=['time_start', 'time_end', 'first_temp_lpn', 'first_temp_lpn_validated', 'second_temp_lpn', 'second_temp_lpn_validated', 'third_temp_lpn', 'third_temp_lpn_validated'])
df2 = df2.append({'time_start' : time_start, 'time_end' : time_end, 'first_temp_lpn' : list_lpn_temp[0][0], 'first_temp_lpn_validated' : list_validated[0], 'second_temp_lpn' : list_lpn_temp[1][0], 'second_temp_lpn_validated' : list_validated[1$  # Your question cut this line off here also.

with engine.connect() as conn, conn.begin():
    df2.to_sql('Raw_Validated', conn, if_exists='append', index=False)

推荐阅读