python - How to convert tuple in string to tuple object? -
in python 2.7, have following string:
"((1, u'central plant 1', u'http://egauge.com/'), (2, u'central plant 2', u'http://egauge2.com/'))"
how can convert string tuples? i've tried use split
few times it's messy , makes list instead.
desired output:
((1, 'central plant 1', 'http://egauge.com/'), (2, 'central plant 2', 'http://egauge2.com/'))
thanks in advance!
you should use literal_eval
method ast
module can read more here.
>>> import ast >>> s = "((1, u'central plant 1', u'http://egauge.com/'),(2, u'central plant 2', u'http://egauge2.com/'))" >>> ast.literal_eval(s) ((1, u'central plant 1', u'http://egauge.com/'), (2, u'central plant 2', u'http://egauge2.com/'))
Comments
Post a Comment