tags:

views:

86

answers:

1

Hello I have a tuple in string that I revive from a PostgreSQL function> I want to convert that to a tuple but it gives me an error with the real string inside the tuple an EOF error, the code it's like this.

eval('(4125, <html>
<body>
Heloo There!
<body>
</html>)')

, this is just an example of the HTML because the real code it's to big. I don't want to do a for because are many character so could put me very slow the system.

I am open to all the ideas except the for or while.

+6  A: 

The problem is that the 'real' string isn't a string.

'(4125, <html>
<body>
Heloo There!
<body>
</html>)'

now remove the single quotes to get

(4125, <html>
<body>
Heloo There!
<body>
</html>)

now remove the parenthesis and the first element

<html>
<body>
Heloo There!
<body>
</html>

See, no string.

And shame on you for using eval on a string from a database. Didn't your parents raise you better?

aaronasterling
Thank you for the answer and you're right, that was a bad use of eval.
hidura
But man all what is inside it's a string, is '4157, "Hello", "End"' i can't use anything to convert that into a tuple.
hidura
data =value.strip("'")print(tuple(data[1:-1])) This was my last idea.
hidura
@hidura, try `data = tuple(str(v) for v in value.split(','))` where `value = '4157, "Hello", "End"'`. This assumes that you want a tuple of strings. If that isn't correct, you'll have to post another question. They're free ;)
aaronasterling
I did this >>> data='(1, 2, "hello\n")'>>> newData = data.strip("()")>>> newData'1, 2, "hello\n"'>>> newTpl = newData.split(', ')>>> newTpl['1', '2', '"hello\n"'], strip gave me a list, that list i used to create the new tuple, thank you for the advice. The data it was already a string.
hidura