ast.literal_eval() 支持 Python 2.7 中的集合文字吗?
在 What's New in Python 2.7 文档中,它说对集合文字的支持是从 Python 3.1 向后移植的。然而,这种支持似乎并未扩展到 ast
模块的 literal_eval()
函数,如下所示。
这是故意的、疏忽的还是其他原因——从字符串表示创建文字集的最干净的解决方法是什么? (我假设以下内容在 Python 3.1+ 中有效,对吧?)
import ast
a_set = {1,2,3,4,5}
print(a_set)
print(ast.literal_eval('{1,2,3,4,5}'))
输出显示错误消息:
set([1, 2, 3, 4, 5])
Traceback (most recent call last):
File "...\setliterals.py", line 4, in <module>
print ast.literal_eval('{1,2,3,4,5}')
File "...\Python\lib\ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "...\Python\lib\ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string
PS 我能想到的唯一解决方法是使用 eval()
。
In the What’s New in Python 2.7 document it says that support for set literals was back-ported from Python 3.1. However it appears that this support was not extended to the ast
module's literal_eval()
function, as illustrated below.
Was this intentional, an oversight, or something else — and what are the cleanest workarounds for creating a literal set from a string representation? (I assume the following works in Python 3.1+, right?)
import ast
a_set = {1,2,3,4,5}
print(a_set)
print(ast.literal_eval('{1,2,3,4,5}'))
Output showing error message:
set([1, 2, 3, 4, 5])
Traceback (most recent call last):
File "...\setliterals.py", line 4, in <module>
print ast.literal_eval('{1,2,3,4,5}')
File "...\Python\lib\ast.py", line 80, in literal_eval
return _convert(node_or_string)
File "...\Python\lib\ast.py", line 79, in _convert
raise ValueError('malformed string')
ValueError: malformed string
P.S. The only workaround I can think of is to use eval()
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我一直在使用它来转换 pandas DataFrame 中的列(
df[col] = df[col].apply(to_set)
。对于发现这个问题的任何人来说可能有用。它可能不像速度很快,但它避免使用eval
。I've been using this for converting columns in a pandas DataFrame (
df[col] = df[col].apply(to_set)
. Might be useful for anyone finding this question. It may not be as fast but it avoids usingeval
.从错误报告中: http://bugs.python.org/issue10091
Raymond Hettinger 说:
我相信我们可以从这篇文档中得出结论,这个问题不一定是一个错误,因为集合文字是从 Python 3.2 向后移植到 3.1 和 2.7 的。这是
ast.literal
的 Python 2.7 用户应该注意的事情。From the bug report: http://bugs.python.org/issue10091
Raymond Hettinger says:
I believe we can conclude from this document that the matter is not necessarily a bug, since the set literal was backported from Python 3.2 to 3.1 and 2.7. It is something that a Python 2.7 user of
ast.literal
should be aware of.