Skip to main content
 首页 » 编程设计

python之从带有空格的字符串解析 int 不同于 py2 到 py3

2025年05月04日26myhome

将带空格的字符串解析为整数从 Python2 更改为 Python3。

在 Python2 中是:

>>> int('-11') 
-11 
>>> int('- 11') 
-11 

而在 Python3 中:

>>> int('-11') 
-11 
>>> int('- 11') 
Traceback (most recent call last): 
  File "<stdin>", line 1, in <module> 
ValueError: invalid literal for int() with base 10: '- 11' 

弄清楚这一点后,我试图在文档中找到有关此更改的一些解释/详细说明,但找不到任何内容。

所以我的问题是:如何修改代码以从 py2 迁移到 py3? i = int(s.replace(' ','')) 是要走的路吗?或者有更好的建议吗?是否有一些我只是没有找到的关于该更改的描述?

请您参考如下方法:

这在 Python 3 中明确更改以响应 issue 1779 :

I discovered that when converting a string to an int or float, the int conversion allows whitespace after the sign, while the float conversion doesn't. I think they should be consistent.

这已在 3.0a3 changelog 中注明(问题编号有错别字):

  • Issue #1769: Now int("- 1") is not allowed any more.

允许空格与其他数字转换不一致。

解决这个问题最快的方法是使用str.replace(),是的:

>>> import timeit 
>>> timeit.timeit('int("- 1".replace(" ", ""))') 
0.37510599600500427 
>>> timeit.timeit('int("- 1".translate(map))', 'map = {32: None}') 
0.45536769900354557 
>>> timeit.timeit('literal_eval("- 1")', 'from ast import literal_eval') 
6.255796805999125 
>>> timeit.timeit('int(extract("- 1"))', 'import re; from functools import partial; extract = partial(re.compile(r"[^\d\.\-]").sub, "")') 
0.7367695900029503 

Python 2.7 文档 was updated after the fact ,通过向后移植 Python 3 文档。它现在明确指出符号和数字之间不应有空格。所以官方不再支持空格,但为了不破坏向后兼容性,这个错误被保留了下来。