将字符串元组转换为元组 [英] Convert a string tuple to a tuple
本文介绍了将字符串元组转换为元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个用户输入的字符串,该字符串已经是元组格式,并且想在 python 中将其转换/转换为实际的元组.我怎样才能做到这一点?例如:
I have a user entered string which is already in tuple format and would like to convert/cast it to a actual tuple in python. How can I do this? E.g:
strTup = '(5, 6)'
想将上面的转换为元组 (5, 6).我尝试了 tuple(strTup)
,但它不起作用,因为它将每个字符都变成了自己的元组.
Would like to convert the above to the tuple (5, 6). I tried tuple(strTup)
which did not work as it made each character into its own tuple.
推荐答案
你可以传递给 eval() :
You can pass it to eval():
就去做:
<!-- language: python -->
strTup = '(5,6)'
eval(strTup)
这篇关于将字符串元组转换为元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文