tos*_*osh 16 python string tuples eval data-structures
我有一些看起来像这样的字符串:
"(8, 12.25), (13, 15), (16.75, 18.5)"
Run Code Online (Sandbox Code Playgroud)
我想将它们中的每一个转换为python数据结构.优选地,包含一对浮点值的元组的列表(或元组).
我可以做到这一点eval("(8, 12.25), (13, 15), (16.75, 18.5)"),给我一个元组元组,但我不认为天真评估外部信息将是一个明智的决定.
所以我想知道优雅的pythonic解决方案是什么样的.
nos*_*klo 24
>>> import ast
>>> print ast.literal_eval("(8, 12.25), (13, 15), (16.75, 18.5)")
((8, 12.25), (13, 15), (16.75, 18.5))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8361 次 |
| 最近记录: |