ValueError: dictionary update sequence element #0 has length 3; 2 is required

ValueError: dictionary update sequence element #0 has length 3; 2 is required

我正在尝试在 MongoDB 中插入值,但出现此错误:

ValueError: dictionary update sequence element #0 has length 3; 2 is required.

From pymongo import MongoClient

client = MongoClient()

db = client.abc_database

keys = []
values = []
key = input("enter keys:").split(",")
keys.append(key)
print(keys)
print(keys[0][1])
value = input("enter values").split(",")
values.append(value)
print(values)
fin = []
k = 0
while k < len(keys[0]):
    d = keys[0][k] + ":" + values[0][k]
    fin.append(d)
    k += 1
print(fin)
fin_id = db.fin.insert(dict([fin]))
print("successful")

您正在尝试通过首先将每个键连接到它的值来从键值对构造一个 dict

d = keys[0][k] + ":" + values[0][k]

将键值对改为元组:

d = keys[0][k], values[0][k]

那么你应该可以使用dict(fin)。请注意 fin 已经是一个列表;不要像在 dict([fin]).

中那样包装在另一个列表中