将 Teradata 查询读入 Pandas

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34948453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 00:33:10  来源:igfitidea点击:

Read Teradata query into Pandas

pythonpandassqlalchemyteradata

提问by atm

Has anyone found a way to read a Teradata query into a Pandas dataframe? It looks like SQLAlchemy does not have a Teradata dialect.

有没有人找到将 Teradata 查询读入 Pandas 数据帧的方法?看起来 SQLAlchemy 没有 Teradata 方言。

http://docs.sqlalchemy.org/en/latest/dialects/

http://docs.sqlalchemy.org/en/latest/dialects/

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html

http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html

采纳答案by minatverma

I did it using read_sql. Below id the code snip :

我是用read_sql. 下面是代码片段:

def dqm() :
    conn_rw = create_connection()
    dataframes = []
    srcfile = open('srcqueries.sql', 'rU').read()
    querylist = srcfile.split(';')
    querylist.pop()
    for query in querylist :
        dataframes.append(pd.read_sql(query, conn_rw))
    close_connection(conn_rw)
    return dataframes,querylist

You can create connection as below :

您可以创建如下连接:

    def create_connection():
        conn = pyodbc.connect("DRIVER=Teradata;DBCNAME=tddb;UID=uid;PWD=pwd;QUIETMODE=YES", autocommit=True,unicode_results=True)
        return conn

You can check complete code here : GitHub LinkLet me know if this answers your query .

您可以在此处查看完整代码:GitHub 链接如果这能回答您的查询,请告诉我。

回答by Prayson W. Daniel

You can use slqalchemy but you will need to install sqlalchemy-teradata too. You can do that via PIP

您可以使用 slqalchemy,但您也需要安装 sqlalchemy-teradata。你可以通过 PIP 做到这一点

pip install sqlachemy-teradata

The rest of the code remains the same :)

其余代码保持不变:)

from sqlalchemy import create_engine
import pandas as pd

user, pasw, host = 'username','userpass', 'hostname'

# connect
td_engine = create_engine('teradata://{}:{}@{}:22/'.format(user,pasw,hostname))

# execute sql
query = 'select * from dbc.usersV'
result = td_engine.execute(query)

#To read your query to Pandas
df = pd.read_sql(query,td_engine)