我正在嘗試通過 python 將我們的數(shù)據(jù)從 Exasol 移動(dòng)到 MongoDB。但我被困在某個(gè)地方。我是 Python 新手,實(shí)際上這是我的第一個(gè)代碼。嘗試插入數(shù)據(jù)時(shí)出現(xiàn)錯(cuò)誤。請你幫助我好嗎?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打印結(jié)果(文檔) { '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貢獻(xiàn)1877條經(jīng)驗(yàn) 獲得超1個(gè)贊
您將您的存儲(chǔ)document為字符串,而不是字典。print(type(document))應(yīng)該返回字符串。
與其手動(dòng)“模擬”字典格式,不如將數(shù)據(jù)放入實(shí)際字典中:
for col in Read:
document = dict()
while y < len(Col_Names):
document[Col_Names[y]] = col[y]
result = db.nyc.insert_one(document)
希望我能幫上忙!
添加回答
舉報(bào)
0/150
提交
取消