I want to export the model data(in bytea format stored in psql database) into a csv and then create a dataframe using pandas.
My code which I tried:
query = ("""SELECT model from {} where col1 = '{}' and col2 = '{}'""").format(
os.environ['table'], os.environ['col2'], hier)
with tempfile.TemporaryFile() as tmpFile:
copy_sql = "COPY ({query}) TO STDOUT WITH CSV {head}".format(
query=query, head="HEADER"
)
conn = engine.raw_connection()
cur = conn.cursor()
cur.copy_expert(copy_sql, tmpFile)
tmpFile.seek(0)
dataFrame = pd.read_csv(tmpFile)
It dataFrame['model'] gives data in string and not bytes-like object format.
Update: Using pd.read_sql command ;
dataFrame = pd.read_sql(query, con=engine, columns=['col1', 'model'])