python - 从字符串到整数的转换

标签 python string python-2.7 converters

我正在通过 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: ''

最佳答案

稍微更pythonic的方式:

i = int(csq) if csq else None

关于python - 从字符串到整数的转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30506897/

相关文章:

python - 如何改进 Python 2.7 中文本文件中的写入流

php - 如何将(大)整数映射到(小尺寸(带有 PHP 的字母数字字符串?(Cantor?)

c - 从 fgets() 输入中删除尾随换行符

python保证文件一致性

python - 如何使用管理员在 Python 中运行 cmd 命令

python - 切诺基导致产生多个 uwsgi 实例的问题

python - 如何在 django 中过滤产品的价格范围?

c# - 无法在 asp mvc 中隐式转换类型“System.Collections.Generic.List”

python - 如何使用 tkinter 比例修改图像?

python - 在我的示例中如何访问类外的变量?