gpt4 book ai didi

python - 我收到了 “nonetype”错误,但我不知道为什么

转载 作者:行者123 更新时间:2023-12-03 08:41:13 27 4
gpt4 key购买 nike

所以我基本上知道错误的含义,但是我不知道为什么得到它。我想将我的cvs文件插入表中。有些行是空的,这就是为什么我使用if None ...的原因。

错误即时消息:

机构中的文件“C:\ Users \ Desktop \ IngestData.py”,第64行
cokey = temp [0]
TypeError:“NoneType”对象不可下标

希望有人能帮助我!
先感谢您!

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])

关于python - 我收到了 “nonetype”错误,但我不知道为什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61308120/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com