python - 如何在Python中动态打印列表

标签 python

我们正在开发一个 Python 项目,从 MySQL 数据库中检索数据,然后将其发送回数据库中的新表。我们已经初始化了一个列表,

list_lpn_temp = []

问题是该列表的范围有所不同,因此我们并不总是知道列表中有多少个数据点。我们有这段代码,这就是错误发生的地方:

df2 = pd.DataFrame(columns=['first_temp_lpn', 'first_temp_lpn_validated', 'second_temp_lpn', 'second_temp_lpn_validated', 'third_temp_lpn', 'third_temp_lpn_validated'])
df2 = df2.append({'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], 'third_temp_lpn' : list_lpn_temp[2][0], 'third_temp_lpn_validated' : list_validated[2]}, ignore_index=True).round(2)

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

有时它会给我们一个错误,指出索引超出范围,因为我们有时列表中只有 2 个值,因此 list_lpn_temp[3][0] 会给我们错误。梦想的场景是,如果我们能够以某种方式发送一个null或者一些文本表明我们的数据库没有任何值(value)。

因此我们需要两件事:

  1. 发送数据,但在哪里取决于我们列表的大小,而不仅仅是设置静态。例如这样(我们需要比这更好的东西):

    'first_temp_lpn' : list_lpn_temp[0][0]

  2. 如果我们收到的索引超出范围,那么我们仍然需要向数据库发送一些内容,因为它需要 3x 列的温度。但由于没有值,我们可以发送 null,因此这可能很好实现。否则我们就会遇到另一个大问题。

代码的更大部分

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

list_lpn_temp = []

index = pd.date_range(start=start_range.min(), end=end_range.max(), freq='20T')

for x in index:
    a_temp = pd.read_sql('SELECT temperature FROM Raw_Data', conn).astype(float).values

list_lpn_temp.extend(a_temp)

if len(list_lpn_temp) > max_samples:
    list_lpn_temp.pop(0)

for i in range (len(list_lpn_temp)):
    if -1.5 < 25-list_lpn_temp[i] < 1.5:
        validated_lpn = 1
        list_validated.append(validated_lpn)
        new_list_lpn_temp.extend(list_lpn_temp[i])
    else:
        validated_lpn = 0
        list_validated.append(validated_lpn)

df2 = pd.DataFrame(columns=['first_temp_lpn', 'first_temp_lpn_validated', 'second_temp_lpn', 'second_temp_lpn_validated', 'third_temp_lpn', 'third_temp_lpn_validated'])
df2 = df2.append({'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], 'third_temp_lpn' : list_lpn_temp[2][0], 'third_temp_lpn_validated' : list_validated[2]}, ignore_index=True).round(2)

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

新(KP)

我们有一个 time_start 和 time_end 值,其格式为日期时间。我们想将其与临时数据一起发送,因此我们尝试修改 df2.append。

lastTime = pd.read_sql('SELECT MAX(timestamp) FROM Raw_Data', conn).astype(str).values.tolist()
firstTime = pd.read_sql('SELECT MIN(timestamp) FROM Raw_Data', conn).astype(str).values.tolist()

firstTime = (pd.to_datetime(firstTime[0])-datetime.timedelta(minutes=10)).round('20T')
lastTime = (pd.to_datetime(lastTime[0])-datetime.timedelta(minutes=10)).round('20T')

test = lastTime - datetime.timedelta(minutes=40)

time_start = test.astype(str).values[0]
lastTime = lastTime + datetime.timedelta(minutes=20)
time_end = lastTime.astype(str).values[0]

for name, value, valid in zip(['first', 'second', 'third'], list_lpn_temp, list_validated):
   temp[name+'_temp_lpn'] = value[0]
   temp[name+'_temp_lpn_validated'] = valid

df2 = df2.append({'time_start' : time_start, 'time_end' : time_end}, temp)        

print (df2)

但是只有日期时间被发送(time_start 和 time_end)

enter image description here

最佳答案

您可以循环遍历列表中的元素。 类似的东西

temp = {}
for name, value in zip(['first', 'second', 'third'], list_lpn_temp):
   temp[name+'_temp_lpn'] = value[0]
   temp[name+'_temp_lpn_validated'] = value[1]
df2 = df2.append(temp)

关于python - 如何在Python中动态打印列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57726869/

相关文章:

python - sikuli可以动态捕捉图像吗?

python - 如何在 Pandas 中将三个字符串列组合为具有Nan值的一列

python - 有没有办法根据字典的值检查成员资格?确定之后,我们还可以做其他事情吗?

python - 在 Python 中,是否可以调用类 A 的实例方法,但传入类 B 的实例?

python - Sage 的 "var"是如何工作的?

python - Apache Airflow 从 1.9.0 迁移到 1.10.1 后出现的问题

python - 使用 django tastypie 创建搜索 API

python - 添加重力到networkx.spring_layout

python - Django 在 S3 中存储上传的文件

Python 从 CSV 创建字典并使用文件名作为键