gpt4 book ai didi

python - 创建扩展数据表

转载 作者:行者123 更新时间:2023-12-01 06:21:00 27 4
gpt4 key购买 nike

我是Python初学者。我发现这段代码非常适合我(将 IP21 连接到 Power BI) - 只是试图增加标签(信息)的数量

import pandas as pd
import pyodbc
conn = pyodbc.connect('NAME')
tag = 'MYTAG'
start = '2019-01-01 12:00:00'
end = '2019-10-02 12:00:00'
sql = "select TS,VALUE from HISTORY "\
"where NAME='%s'"\
"and PERIOD = 60*10"\
"and REQUEST = 2"\
"and REQUEST=2 and TS between TIMESTAMP'%s' and TIMESTAMP'%s'" % (tag, start, end)
data = pd.read_sql(sql,conn) # Pandas DataFrame with your data!

而不是这个,我只想要 tag1、tag2...并获取数据,该数据是一个包含日期和所有这些值的表。如何更改代码?我得到了一个使代码运行的答案,但我认为我的续集编程被搞乱了。它不是将名称作为一个标签,而是使搜索名称 (tag1,tag2) 不是有效的搜索名称。你如何在续集中找到多个东西?我想将其更改为

tag = ' TAG1'
tag2 =' TAG2'
start = '2019-01-01 12:00:00'
end = '2019-10-02 12:00:00'
sql = "select TS,VALUE from HISTORY "\
"where NAME=('%s,%s)'"\
"and PERIOD = 60*10"\
"and REQUEST = 2"\
"and REQUEST=2 and TS between TIMESTAMP'%s' and TIMESTAMP'%s'" % (tag, tag2, start, end)
data = pd.read_sql(sql,conn) # Pandas DataFrame with your data!

类似的东西。这有效,但给出了一个空表。谢谢!

最佳答案

如果我理解得好的话。您想要一些标签的信息。因此,只需调整查询中标签的条件即可。您应该注意到,每个标签都有一个 %s。因此,如果要添加标签,请在查询中添加%s,并在括号中的%后面添加相应的变量。尝试了解谁在 python 中进行刺痛替换。这很容易。

import pandas as pd
import pyodbc
conn = pyodbc.connect('NAME')
tag = 'MYTAG'
start = '2019-01-01 12:00:00'
end = '2019-10-02 12:00:00'
sql = "select TS,VALUE from HISTORY "\
"WHERE NAME in ('%s', '%s')"\
"and PERIOD = 60*10"\
"and REQUEST = 2"\
"and REQUEST=2 and TS between TIMESTAMP'%s' and TIMESTAMP'%s'" % (tag, start, end)
data = pd.read_sql(sql,conn) # Pandas DataFrame with your data!

关于python - 创建扩展数据表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60356132/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com