开发者

Converting strings into another data type, Python

开发者 https://www.devze.com 2023-04-12 18:37 出处:网络
I have the string \"(0, 0, 0)\". I\'d like to be able to convert this to a tuple. The built in tuple function doesn\'t work for my purposes because it treats each character as an individual item. I wa

I have the string "(0, 0, 0)". I'd like to be able to convert this to a tuple. The built in tuple function doesn't work for my purposes because it treats each character as an individual item. I want 开发者_开发百科to be able to convert "(0, 0, 0)" to (0, 0, 0) programmatically.


You can use ast.literal_eval

>>> import ast
>>> ast.literal_eval('(0,0,0)')
(0, 0, 0)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号