So when I run:
value = long("00000000000000020000000000000002", 16)
I get :
ValueError: Value out of range: 36893488147419103234
I think it's because long can't take such a big hex number, but I'm not sure.
In reality I'm iterating through a file with a large amount of very big hex numbers, but this is just an example of one of the hex numbers I'm trying to parse.
I've tried using lstrip() to remove some of the 0's but it made no difference to the error.
What am I doing wrong?
The error was being caused by the variable I was trying to assign the value to, not the actual long() function.
Related
I have the following list:
Attributes are listed in first row and dummy variables check whether a certain candy type fits this critea. The last column shows the "success value" of a certain candy type in each row
I read that using the map function would be "the most elegant, pythonic and recommended method to perform this particular task."
So attempted to apply the map function to my list (here stored as 'data') as follows:
data_int=list(map(int, data)) print(data_int)
However, I get the error code
"ValueError: invalid literal for int() with base 10: 'Whoppers'"
for the first line.
("Whoppers" being the first element of the last line of the table)
Can anyone please explain me my error and what to do in order to resolve this issue?
It is a little unclear what you are trying to do. Could you add an example of what data contains?
The error is generated because you are attempting to convert every element in data to an int. This includes the names of the candies, which is not possible to convert to a numerical value, causing the error message.
If you run int("Whopper") in the interpreter you will get this error message.
If data is in the form: ["Whopper", "0", "0", "1", "0", "1"] then running list(map(int,data[1:])) will give you the result that I think you are looking for, since the operator [1:] excludes the first element of the list (in your case, the name of the candy).
The following are totally acceptable in python:
passing a string representation of an integer into int
passing a string representation of a float into float
passing a string representation of an integer into float
passing a float into int
passing an integer into float
You are getting a ValueError cause you are passing a string representation of a float into int. If you still want to pass a string representation of a float to an int you can typecast to a float first, then to an integer like this
int(float('11.000000'))
I have seen many posts here, which gives ways of removing the trailing L from a list of python long integers.
The most proposed way is
print map(int,list)
However this seems not to work always.
Example---
A=[4198400644L, 3764083286L, 2895448686L, 1158179486, 2316359001L]
print map(int,A)
The above code gives the same result as the input.
I have noticed that the map method doesn't work whenever the number preceding L is large, and only when the numbers are in a list. e.g. Application of int() on 4198400644L does give the number without L, when out of the list.
Why is this occurring and more importantly, how to overcome this?
I think I really need to remove this L, because this is a small part of a program where I need to multiply some integer from this list A, with some integer from a list of non-long integers, and this L is disturbing.I could ofcourse convert the long integers into string,remove the L and convert them back to integer.But is there another way?
I am still using the now outdated Python 2.7.
Python has two different kinds of integers. The int type is used for those that fit into 32 bits, or -0x80000000 to 0x7fffffff. The long type is for anything outside that range, as all your examples are. The difference is marked with the L appended to the number, but only when you use repr(n) as is done automatically when the number is part of a list.
In Python 3 they realized that this difference was arbitrary and unnecessary. Any int can be as large as you want, and long is no longer a type. You won't see repr put the trailing L on any numbers no matter how large, and adding it yourself on a constant is a syntax error.
This question already has answers here:
How to use digit separators for Python integer literals?
(4 answers)
Closed 3 years ago.
Unable to convert a string to an integer. Getting Error:
ValueError: invalid literal for int() with base 10: '2674'
I have read many answers and most of them the reason for not working is because it is a float and people use int(), but here clearly it is an integer.
like1 = '2,674 likes'
number = int(like1.split()[0])
print('This is the numer ::',number)
I expect the code to run without an error and print out the number. My actual implementation is to compare it with an integer. For eg. number > 1000, but that throws up the error that you can't compare a string and int, therefore I wanted to modify it into an int. I did not want to post that code since it was pretty big and messy.
Please ask for clarifications, if I have missed any required details!
Your problem is the comma in 2,674. Some locations use that as a decimal point, but your location does not, and you want it to separate groups of three digits in an integer. Python does not use it in that way.
So either remove the comma or change it to an underscore, _, which recent versions of Python allow in an integer. Since I do not know your version of Python I recommend removing the comma. Use this:
number = int(like1.split()[0].replace(',', ''))
By the way, the error message you show at the top of your question is not the actual error message. The actual message shows the comma in the string:
ValueError: invalid literal for int() with base 10: '2,674'
If you continue asking questions on this site, be sure to show the actual errors. In fact, you should copy and paste the entire traceback, not just the final line. That will help us to correctly understand your problem and help you.
I am extracting a string out of a JSON document using python that is being sent by an app in development. This question is similar to some other questions, but I'm having trouble just using x = ast.literal_eval('[0448521958, +61439800915]') due to the plus sign.
I'm trying to get each phone number as a string in a python list x, but I'm just not sure how to do it. I'm getting this error:
raise ValueError('malformed string')
ValueError: malformed string
your problem is not just the +
the first number starts with 0 which is an octal number ... it only supports 0-7 ... but the number ends with 8 (and also has other numbers bigger than 8)
but it turns out your problems dont stop there
you can use regex to fix the plus
fixed_string = re.sub('\+(\d+)','\\1','[0445521757, +61439800915]')
ast.literal_eval(fixed_string)
I dont know what you can do about the octal number problem however
I think the problem is that ast.literal_eval is trying to interpret the phone numbers as numbers instead of strings. Try this:
str = '[0448521958, +61439800915]'
str.strip('[]').split(', ')
Result:
['0448521958', '+61439800915']
Technically that string isn't valid JSON. If you want to ignore the +, you could strip it out of the file or string before you evaluate it. If you want to preserve it, you'll have to enclose the value with quotes.
I am grabbing IP addresses from a site and part of the result looks like this.
192.168.1.1\t12345\t12345\t2013-05-14\t2013-05-14\n192.168.1.1\t98765\t98765\t2013-05-14\t2013-05-14
And of course the complete result have a lot of IP addresses. I'm trying to change that particular string into int so that I can get the length. I've tried
length = int(string, 16)
but it's giving me invalid literal for int() with base 16 error. How can I change it into int?
If you want to get the length of the whole string you can just do len(string). If you want to get the number of IP addresses, then you have to split by \n and then get the length of the returned list.
len(string.splitlines())