【发布时间】:2021-12-06 12:50:39
【问题描述】:
我正在尝试从 MySQL 表中读取数据,其中一列包含较大的 varchar 值,例如长度49085。当我将查询结果读入数据框时,列值被截断为 87 个字符。请参阅下面的代码和输出。有谁知道我如何在不截断的情况下读取整个字符串?
在下面的代码中,表 test 包含一列 description,其中一行的字符串长度为 49085。
代码:
import sys
import os
from sqlalchemy import create_engine
import pandas as pd
db_connection_str = 'mysql+pymysql://username:password@host/db_name'
db_connection = create_engine(db_connection_str)
#this returns 1 row where the value in the description field is of length 49085
df = pd.read_sql("select id, description, length(description) as len from myTable where length(description) = 49085", con=db_connection)
#this returns the truncated value of length 87
print(df)
len(str(df['description']))
输出:
id description len
0 1 This document is for the testing Team.\n\nThe attach... 49085
87
【问题讨论】:
-
你雇过别的司机吗?
-
我没有,对此了解不多。你的意思是尝试
sqlalchemy以外的东西吗? -
是的,试试 mysql.connector
标签: python mysql sql pandas sqlalchemy