简体   繁体   中英

Python - Obtain a list of parenthetical tuples from string

I have a string such as: (1,2,3,'4.1),(4.2)',5,6,7),(8,9,10) . The output I need to obtain is the list: [ ((1,2,3,'4.1),(4.2)',5,6,7), (8,9,10) ] I believe I need a regex in order to perform this task. How can I do so?

Thank you.

You might be able to evaluate the string directly (after putting it in a list).

from ast import literal_eval

string = "(1,2,3,'4.1),(4.2)',5,6,7),(8,9,10)"

literal_eval('[{}]'.format(string))
# [(1, 2, 3, '4.1),(4.2)', 5, 6, 7), (8, 9, 10)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM