Math with unicode strings?

irstas at gmail.com irstas at gmail.com
Mon Apr 2 15:15:33 EDT 2007


On Apr 2, 10:09 pm, "erikcw" <erikwickst... at gmail.com> wrote:
> 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




More information about the Python-list mailing list