Convert a string to dictionary - without quoting the values

93 Views Asked by At

I have a dataframe with some column names and data types as following:

column_name                                   data_type 
modifiedDate                                  DateTime
city                                          VARCHAR(50)
state                                         VARCHAR(50)
region                                        VARCHAR(50)

In order to use it with pandas to_sql() I need to send a dictionary using the column names and the datatypes in the following format:

{'modifiedDate': DateTime,
 'city': VARCHAR(60),
 'state': VARCHAR(60),
 'region': VARCHAR(30)}

I have tried to use zip and other methods to convert to a dictionary but it uses single quotes for the values, which causes the to_sql to fail.

col_type = dict(zip(df['column_name'],df['data_type']))

{'modifiedDate': 'DateTime',
 'city': 'VARCHAR(60)',
 'state': 'VARCHAR(60)',
 'region': 'VARCHAR(30)'}

I have also tried to concatenate the values, then use .tolist() and join, but this also fails because it is a string and to_sql expects a dict.

col_list = '{' + ', '.join([str(elem) for elem in values]) + '}'


'{"addressid":Integer, "addressline1":VARCHAR(60), "addressline2":VARCHAR(60), "city":VARCHAR(30), "stateprovince":VARCHAR(50), "countryregion":VARCHAR(50), "postalcode":VARCHAR(15), "modifieddate":DateTime}'

Any help to achieve this would be much appreciated.

0

There are 0 best solutions below