ValueError: invalid literal for int():

L

lee

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
 
C

Chris Rebert

Hi,

I have a value,

partintid = int(Screw plugg  (91_10 -> untitled))

but i get ValueError: invalid literal for int(): Screw plugg  (91_10 -
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
 
C

Chris Rebert

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

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
 
S

Steven D'Aprano

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,724,622,858,761,848,595,862,392,584,788,047,651,881


Obviously.
 
L

lee

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,724,622,858,761,848,595,862,392,584,788,047,651,881

Obviously.

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.
 

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,731
Messages
2,569,432
Members
44,834
Latest member
BuyCannaLabsCBD

Latest Threads

Top