data = st['xy'][0]
x, y = data.split(" ")
x, y = float(x[1:-1]), float(y[:-1]) # Slice parentheses and comma away
还可以使用正则表达式(此方法更通用):
import re
data = st['xy'][0]
number_pattern = r'\d+(?:\.\d+)'
x, y = re.findall(number_pattern, data) # NB: Will error out if your string is not well-formatted
使用ast.ast_literal
literal提供从字符串安全构造对象(即比eval更安全)
看起来您的数据是一个字符串。如果其格式始终与所示格式相似,则可以执行以下操作:
还可以使用正则表达式(此方法更通用):
您可以使用内置模块^a1}:
相关问题 更多 >
编程相关推荐