Converting string to tuple and adding to tuple
问题 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)) 回答1: 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. 回答2: Instead of using a regex or int/string