[Python] Trying to build dtype input dictionary for pandas .to_sql dynamically

Discussão em 'Python' iniciado por Stack, Julho 11, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to use the dataframe column max string lengths to build the something like this:

    dtype={'column1' : sqlalchemy.types.VARCHAR(5000),
    'column2' : sqlalchemy.types.VARCHAR(5000),
    'column3' : sqlalchemy.types.VARCHAR(25000)
    }


    I have tried a couple of different ways:

    str_length = (dict([(v, task_merge[v].apply(lambda r: len(str(r)) if r!=None else 0).max())for v in task_merge.columns.values]))


    That part works to give me a dictionary of all the string max length.

    Here is the part that is not working to add the sqlalchemy.types.VARCHAR() to them:

    db_dict = {f'sqlalchemy.types.VARCHAR({v})' : v for k, v in str_length.items()}


    Results in "The type of sqlalchemy.types.VARCHAR(8) is not a SQLAlchemy type"

    db_dict = {sqlalchemy.types.VARCHAR(v) : v for k, v in str_length.items()}


    Reults in "The type of VARCHAR(8) is not a SQLAlchemy type"

    db_dict = {'''sqlalchemy.types.VARCHAR({v})''' : v for k, v in str_length.items()}


    Reults in "The type of VARCHAR(8) is not a SQLAlchemy type"

    Anyone have any ideas on how to do this?

    Continue reading...

Compartilhe esta Página