Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > ValueError: invalid literal for int():

Reply
Thread Tools

ValueError: invalid literal for int():

 
 
lee
Guest
Posts: n/a
 
      07-26-2010
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?

-
Sunny

 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      07-26-2010
On Mon, Jul 26, 2010 at 3:25 AM, lee <(E-Mail Removed)> 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/stdty...string-methods

Regards,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      07-26-2010
> On Mon, Jul 26, 2010 at 4:25 PM, Chris Rebert <(E-Mail Removed)> wrote:
>> On Mon, Jul 26, 2010 at 3:25 AM, lee <(E-Mail Removed)> 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?

<snip>
>> 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/stdty...string-methods


On Mon, Jul 26, 2010 at 4:03 AM, Sunny chilgod <(E-Mail Removed)> wrote:
> 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.


Nope, still vague. Which is your desired value for partintid: 91? 10?
9110? "91_10"? Something else?

Also, not to be unfriendly, but just note for future reference that
top-posting ( http://en.wikipedia.org/wiki/Top-posting ) is generally
avoided on this mailinglist/newsgroup.

Cheers,
Chris
--
http://blog.rebertia.com
 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      07-26-2010
On Mon, 26 Jul 2010 04:12:33 -0700, Chris Rebert wrote:

> On Mon, Jul 26, 2010 at 4:03 AM, Sunny chilgod <(E-Mail Removed)>
> wrote:
>> 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.

>
> Nope, still vague. Which is your desired value for partintid: 91? 10?
> 9110? "91_10"? Something else?



Well, if you interpret 'Screw plugg (91_10 -¬*untitled)' as a base-256
number, the correct answer is:

147,334,663,384,405,567,160,096,068,524,905,866,72 4,622,858,761,848,595,862,392,584,788,047,651,881


Obviously.



--
Steven
 
Reply With Quote
 
lee
Guest
Posts: n/a
 
      07-26-2010
On Jul 26, 4:30*pm, Steven D'Aprano <st...@REMOVE-THIS-
cybersource.com.au> wrote:
> On Mon, 26 Jul 2010 04:12:33 -0700, Chris Rebert wrote:
> > On Mon, Jul 26, 2010 at 4:03 AM, Sunny chilgod <(E-Mail Removed)>
> > wrote:
> >> 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.

>
> > Nope, still vague. Which is your desired value for partintid: 91? 10?
> > 9110? "91_10"? Something else?

>
> Well, if you interpret 'Screw plugg (91_10 -*untitled)' as a base-256
> number, the correct answer is:
>
> 147,334,663,384,405,567,160,096,068,524,905,866,72 4,622,858,761,848,595,862,392,584,788,047,651,881
>
> Obviously.
>
> --
> Steven


Hi,

i got the value wrong. sorry for the mistake. i get a int value like
"01", so no issue converting it to integer.
thanks.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
RE: Invalid Literal from MySQLdb Query Ben Last Python 0 09-28-2004 05:47 AM
Invalid Literal from MySQLdb Query Wesley Kincaid Python 0 09-27-2004 11:42 PM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM
MySQLdb returns "ValueError: invalid literal for float():" for cursor.execute('show databases') Tim Williams Python 2 05-28-2004 12:48 PM
bug in Tkinter? invalid literal for int() line 1035 Ton K. Python 3 07-07-2003 09:59 AM



Advertisments