我收到 "nonetype" 错误,但我不知道为什么

Im getting the "nonetype" error but i cant figure out why

所以我基本上知道错误是什么意思,但我不知道为什么我 it.I 想将我的 cvs 文件插入 table。有些行是空的,这就是为什么我使用 if None...

我得到的错误:

文件 "C:\Users\Desktop\IngestData.py",第 64 行,在机构中 cokey = temp[0] 类型错误:'NoneType' 对象不是下标table

希望有人能帮助我! 提前致谢!

def institutions(cur):
    with open('persons.csv', 'r', encoding="utf-8") as g:
        #tempo = None
        reader = csv.reader(g)
        next(reader)
        for row in reader:
            if row[4] is None:
                break
            else:
                cur.execute("SELECT COKEY FROM countries WHERE Name = (%s)", [row[4]])
                temp = cur.fetchone()
                cokey = temp[0]
                cur.execute("""INSERT INTO institutions (Name,cokey) VALUES (%s,%s)
                            ON CONFLICT DO NOTHING;""",[row[3],cokey])

您的数据库中是否有匹配的行? cur.fetchone() 返回 None,这就是为什么您的错误是 NoneType is not suscriptable

def institutions(cur):
    with open('persons.csv', 'r', encoding="utf-8") as g:
        #tempo = None
        reader = csv.reader(g)
        next(reader)
        for row in reader:
            if row[4] is None:
                break
            else:
                cur.execute("SELECT COKEY FROM countries WHERE Name = (%s)", [row[4]])
                temp = cur.fetchone()
                if temp:
                    cokey = temp[0]
                    cur.execute("""INSERT INTO institutions (Name,cokey) VALUES (%s,%s) ON CONFLICT DO NOTHING;""",[row[3],cokey])

将行 if row[4] is None 条件更改为 if row[4] is not None,如下所示:

for row in reader:
    if row[4] is not None:
        cur.execute("SELECT COKEY FROM countries WHERE Name = (%s)", [row[4]])
                temp = cur.fetchone()
                cokey = temp[0]
                cur.execute("""INSERT INTO institutions (Name,cokey) VALUES (%s,%s)
                            ON CONFLICT DO NOTHING;""",[row[3],cokey])