【问题标题】:Python typecasting string to int failsPython将字符串类型转换为int失败
【发布时间】:2015-05-15 10:19:16
【问题描述】:

当我尝试在 python (2.7) 中进行从字符串到整数的简单类型转换时出现错误:

year = device.time[year_pos:].strip() # This is '1993'
t = int(year) # Throws exception: "UnboundLocalError: local variable 'int' referenced before assignment"

为什么? :)

【问题讨论】:

    标签: python integer typeconverter


    【解决方案1】:

    UnboundLocalError 听起来好像您在代码的其他范围内为名称 int 分配了一些东西(即,引发异常的代码在函数内部,并且您使用 int 作为在全局代码或其他地方存储变量的名称)。在a blog post about UnboundLocalError 可以阅读更多关于此类问题的信息,但对于您的问题,我建议不要使用内置名称来存储变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多