【发布时间】:2014-06-03 14:25:23
【问题描述】:
我有一个 Python 字典,我正试图将它插入 mysql。问题是字典的键之一长于 64 个字符(mysql 中列的最大长度)。所以我需要将所有字典键截断为 64 个字符。
下面的代码在所有方面都有效,除了一个超过 64 个字符的键 = location_of_building_on_the_lot_if_garage_change_type_code_to_bgd_
data = {'x_coordinate': '1158020.73068669',
'any_people_using_property_homeless_childen_gangs_': True,
'police_district': '8',
'location_of_building_on_the_lot_if_garage_change_type_code_to_bgd_': 'Front',
'service_request_number': '14-00630589',
'address_street_suffix': 'AVE',
'y_coordinate': '1866585.99638448',
'date_service_request_was_received': '2014-05-01T00:00:00',
'address_street_number': '5719',
'longitude': '-87.69612590561026',
'latitude': '41.78965826126179',
'address_street_name': 'FRANCISCO',
'address_street_direction': 'S',
'location': {'latitude': '41.78965826126179', 'needs_recoding': False, 'longitude': '-87.69612590561026'},
'service_request_type': 'Vacant/Abandoned Building',
'community_area': '63',
'is_the_building_currently_vacant_or_occupied_': 'Vacant',
'ward': '16',
'is_building_open_or_boarded_': 'Open',
'is_the_building_vacant_due_to_fire_': True,
'zip_code': '60629'}
placeholders = ', '.join(['%s'] * len(data))
columns = ', '.join(data.keys())
sql = "INSERT INTO vacant_buildings (%s) VALUES (%s)" % (columns, placeholders)
我试图改变:
columns = ', '.join(data.keys())
到
columns = ', '.join(data[:64].keys())
但得到以下错误:TypeError: unhashable type
想法?
【问题讨论】:
标签: python dictionary truncate