ValueError: invalid literal for int():

Sunny chilgod san82moon at gmail.com
Mon Jul 26 07:03:19 EDT 2010


Hi Chris,

Thanks for your help. but i need to to convert the whole string to int.

heres my full code,

ptid = 'item_01bom'
so item_01bom is a field name in form, so i get its value,
partintid = int(form[ptid]).  # the value of form[ptid] is 'Screw plugg
 (91_10 - untitled)'

Hence i get the error. hope i am clear now.

-
Sunny


On Mon, Jul 26, 2010 at 4:25 PM, Chris Rebert <clp2 at rebertia.com> wrote:

> On Mon, Jul 26, 2010 at 3:25 AM, lee <san82moon at gmail.com> wrote:
> > Hi,
> >
> > I have a value,
> >
> > partintid = int(Screw plugg  (91_10 -> untitled))
> >
> > but i get ValueError: invalid literal for int(): Screw plugg  (91_10 -
> >> untitled)
> > any help?
>
> That is most certainly not your actual exact code, since it has a few
> SyntaxErrors and thus Python would have bailed-out long before it ever
> got the chance to raise ValueError. Please copy-and-paste the *actual
> exact code* and exact error message.
> Also, next time say what the desired output/behavior you're seeking is.
>
> That being said, if your code is (as I suspect) in actuality:
>
> partintid = int("Screw plugg  (91_10 -> untitled)")
>
> then I would agree with int() and say that that string is nowhere
> close to representing an integer (how precisely is "Screw" to be
> interpreted as an integer, pray tell?); so what's so surprising about
> getting an error when trying to convert it to one?
>
> I suspect you're trying to extract 91 or 10 from the string. Use
> string methods[1] to parse the desired numerical section out of the
> string, and then pass the resulting numerical string to int(), which
> will accept it without error and properly convert it.
>
> If you want more detailed help, please provide a specification of
> typical input strings and desired output integers.
>
> [1]: http://docs.python.org/library/stdtypes.html#string-methods
>
> Regards,
> Chris
> --
> http://blog.rebertia.com
>



-- 
All that we are is the result of what we have thought. The mind is
everything. What we think we become.

Regards,
Sunny
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100726/ef7874f9/attachment.html>


More information about the Python-list mailing list