Python 3.6-';非类型';测试失败
我正在从Python 3.6-';非类型';测试失败,python,python-3.x,Python,Python 3.x,我正在从sqlite3数据库中读取数据并过滤掉NoneType,因为我只想要而不是None的值。我尝试了建议的两种方法,得到了相同的结果。这让我觉得下面的if语句是正确的,但我遗漏了一些更基本的东西。如有任何建议,我们将不胜感激 从数据库读取 非定型试验 错误 TypeError:int()参数必须是字符串、类似字节的对象或数字,而不是“NoneType” 如果我在我的if语句中专门标记而不是None类型,为什么会出现此错误 正确的方法是什么,例如当值为None时,我可以指定预定义的值 您可以
sqlite3
数据库中读取数据并过滤掉NoneType
,因为我只想要而不是None
的值。我尝试了建议的两种方法,得到了相同的结果。这让我觉得下面的if语句是正确的,但我遗漏了一些更基本的东西。如有任何建议,我们将不胜感激
从数据库读取
非定型试验
错误
TypeError:int()参数必须是字符串、类似字节的对象或数字,而不是“NoneType”
- 如果我在我的
if语句中专门标记
类型,为什么会出现此错误李>而不是None
- 正确的方法是什么,例如当值为
时,我可以指定预定义的值李>None
- 正确的方法是什么,例如当值为
None
,这是您实际试图转换为整数的内容:
if current_tact is not None and current_tact[0] is not None:
current_tact = int(current_tact[0])
或者使用try except
块来避免测试:
try:
current_tact = int(current_tact[0])
except TypeError:
current_tact = 60
是
current\u-tact=[None]
(或包含None
作为第一个元素的列表/元组)?似乎current\u-tact中不存在任何元素。尝试print(current\u-tact)
,您会立即看到问题。@hiroprotagent,绝对正确。输出是:(无,)
谢谢你指引我正确的方向,阿兰菲,这是我这方面的基本错误。
current_tact = int(current_tact[0])
if current_tact is not None and current_tact[0] is not None:
current_tact = int(current_tact[0])
try:
current_tact = int(current_tact[0])
except TypeError:
current_tact = 60