将字符串转换为元组并添加到元组 [英] Converting string to tuple and adding to tuple
本文介绍了将字符串转换为元组并添加到元组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个这样的配置文件.
[rects]rect1=(2,2,10,10)rect2=(12,8,2,10)
我需要遍历这些值并将它们转换为元组.然后我需要制作一个元组的元组,如
((2,2,10,10), (12,8,2,10))
解决方案
要将字符串转换为整数元组(我假设这是您想要的),您可以使用这样的正则表达式:
x = "(1,2,3)"t = tuple(int(v) for v in re.findall("[0-9]+", x))
您可以使用例如 configparser 来解析配置文件.>
I have a config file like this.
[rects]
rect1=(2,2,10,10)
rect2=(12,8,2,10)
I need to loop through the values and convert them to tuples. I then need to make a tuple of the tuples like
((2,2,10,10), (12,8,2,10))
解决方案
To turn the strings into tuples of ints (which is, I assume, what you want), you can use a regex like this:
x = "(1,2,3)"
t = tuple(int(v) for v in re.findall("[0-9]+", x))
And you can use, say, configparser to parse the config file.
这篇关于将字符串转换为元组并添加到元组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文