我正在尝试通过 python 将我们的数据从 Exasol 移动到 MongoDB。但我被困在某个地方。我是 Python 新手,实际上这是我的第一个代码。尝试插入数据时出现错误。请你帮助我好吗?while x < len(Read): col = Read[x] while y < len(Col_Names): column_name = "'" + Col_Names[y] + "'" if col[y].isnumeric() or re.match("^\d+?\.\d+?$", col[y]): column_value = col[y] else: column_value = "'" + col[y] + "'" field = column_name + " : " + column_value data.append(field) doc = str(data)[1:-1] document = "{" + doc + "}" y += 1 for char in '"': document = document.replace(char,'') print(document) result = db.nyc.insert_one(document) print('Created {0} of 100 as {1}'.format(x,result.inserted_id)) y = 0 x += 1打印结果(文档) { 'VENDOR_ID': 'CMT', 'PICKUP_DATETIME': '2014-01-09 20:45:25.000000', 'DROPOFF_DATETIME': '2014-01-09 20:52:31.000000', 'PASSENGER_COUNT': 1, 'TRIP_DISTANCE': 0.7, 'PICKUP_LONGITUDE': '-73.99477', 'PICKUP_LATITUDE': 40.736828, 'RATE_CODE': 1, 'STORE_AND_FWD_FLAG': 'N', 'DROPOFF_LONGITUDE': '-73.982227', 'DROPOFF_LATITUDE': 40.73179, 'PAYMENT_TYPE': 'CRD', 'FARE_AMOUNT': 6.5, 'SURCHARGE': 0.5, 'MTA_TAX': 0.5, 'TIP_AMOUNT': 1.4, 'TOLLS_AMOUNT': 0, 'TOTAL_AMOUNT': 8.9, 'COUNTER': 1, 'PATH_ID': 1}
1 回答
冉冉说
TA贡献1877条经验 获得超1个赞
您将您的存储document为字符串,而不是字典。print(type(document))应该返回字符串。
与其手动“模拟”字典格式,不如将数据放入实际字典中:
for col in Read:
document = dict()
while y < len(Col_Names):
document[Col_Names[y]] = col[y]
result = db.nyc.insert_one(document)
希望我能帮上忙!
添加回答
举报
0/150
提交
取消