我正在通过COM端口与调制解调器通信以接收CSQ值。你知道吗
response = ser.readline()
csq = response[6:8]
print type(csq)
返回以下内容:
<type 'str'> and csq is a string with a value from 10-20
为了进一步计算,我尝试将“csq”转换成整数,但是
i=int(csq)
返回以下错误:
invalid literal for int() with base 10: ''
Tags:
一种稍微更像Python的方式:
或者,您可以将代码放入try except块中:
错误消息显示您正试图将空字符串转换为
int
,这将导致问题。你知道吗将代码包装在if语句中以检查空字符串:
请注意,在Python中,空对象(空列表、元组、集、字符串等)的计算结果为
False
。你知道吗相关问题 更多 >
编程相关推荐