Math with unicode strings?

E

erikcw

Hi,

I'm parsing xml data with xml.sax and I need to perform some
arithmetic on some of the xml attributes. The problem is they are all
being "extracted" as unicode strings, so whenever I try to perform
math operations on the variables, I get this error:

cr[0] = data['cls1']/data['ims1'];
TypeError: unsupported operand type(s) for /: 'unicode' and 'unicode'

What do I need to do to extract the intergers from these unicode
strings (or better yet, parse them as intergers in the first place.).
I'm using the SAX method attrs.get('cls1',"") to parse the xml. Can I
"cast" the string into an interger?

Thanks!
Erik
 
I

irstas

Hi,

I'm parsing xml data with xml.sax and I need to perform some
arithmetic on some of the xml attributes. The problem is they are all
being "extracted" as unicode strings, so whenever I try to perform
math operations on the variables, I get this error:

cr[0] = data['cls1']/data['ims1'];
TypeError: unsupported operand type(s) for /: 'unicode' and 'unicode'

What do I need to do to extract the intergers from these unicode
strings (or better yet, parse them as intergers in the first place.).
I'm using the SAX method attrs.get('cls1',"") to parse the xml. Can I
"cast" the string into an interger?

Thanks!
Erik

int(u'123') == 123
float(u'123') == 123.0
 
T

Tim Roberts

erikcw said:
What do I need to do to extract the intergers from these unicode
strings (or better yet, parse them as intergers in the first place.).
I'm using the SAX method attrs.get('cls1',"") to parse the xml. Can I
"cast" the string into an interger?

When I see a typo once, I figure it's a typo, but when I see it three
times, I figure it is a misunderstanding. The word is "integer", not
"interger".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top