i am new in python, i just try to play a video in avg player through python. All the videos are played successfully, but one video has through this value error. i am not sure why this error was happened . if you know describe me.
The specific problem arises because the software tries to interpret 107.24 as an integer number, which it is not.
Why it does this, or where this number is coming from, is really hard to tell from the little information given in your question.
'107.24' is a float string and int() can't convert a float string, use float().
>>> a='107.24'
>>> int(a)
Traceback (most recent call last):
File "<pyshell#1>", line 1, in <module>
int(a)
ValueError: invalid literal for int() with base 10: '107.24'
>>> float(a)
107.24
Related
I have a list with values that should be number. Right now they are an object however:
later object
opstarten object
dtype: object
I have tried to change the column to a str type by doing:
df_analyse_num[["later"]] = df_analyse_num[["later"]].astype(str)
This does not seem to work however cause when I analyse my types it still says object.
Also when I try to convert it to a string something goes wrong. If I do:
df_analyse_num[["later"]] = df_analyse_num[["later"]].astype(str).astype(int)
It gives me the following error:
File "pandas\lib.pyx", line 937, in pandas.lib.astype_intsafe (pandas\lib.c:16667)
File "pandas\src\util.pxd", line 60, in util.set_value_at (pandas\lib.c:67540)
ValueError: invalid literal for int() with base 10: '30.0'
Any thoughts where this goes wrong?
Not an expert on pandas, but try float first to handle the decimal point which indicates a float, then int:
something.astype(str).astype(float).astype(int)
Here is the problem in "native" python:
int('30.0')
Which fails similarly:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '30.0'
If we use float first it works since converting float to int is possible:
int(float('30.0'))
Expected result:
30
My program is supposed to download a file from the internet and then to guess at a persons salary based on certain factors such as age, work, etc. It seems to me that it is not letting me turn the string into an int which I need to do. As I'm still new to python, any help would be appreciated. The main error occurs here:
below_count = 0
for row in myfile:
if ages_midpoint > int(row[0]):
count_below50+=1
The error is:
ValueError: invalid literal for int() with base 10: ''
The error message tells you exactly what is wrong.
If you're looping over a text file with for..in, that means the value row is a string (one line from the file).
You are looking at row[0] which is the first character of the string.
That character is a space (I assume, because calling [0] on the empty string would throw an exception), which is not a legal representation of a decimal number.
You need to go back to what you're actually trying to do and re-think how to do it, because this isn't it.
This might help.
>>> int('3')
3
>>> int('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
so, you can strip off any whitespace and put a try..catch.
below_count = 0
for row in myfile:
try:
if ages_midpoint > int(row.strip()[0]):
count_below50+=1
except ValueError:
# row[0] is not an integer character
# do something here
pass
The value of row[0] is an empty string. You can recreate the error by doing the following on the command line interpreter...
>>> int('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: ''
You might want to validate the value stored in row. if row: will do it.
Unfortunately there's not enough information in your post to be able to help you much beyond that. the line of code if ages_midpoint > int(row[0]): is very suspicious. Is row a line of text from a text file, if so row[0] will return the first character... probably not what you want. Use the string split function word = row.split(<charToSplitOn>)[0]
Hi I'm getting TypeError and i just don't know why...
x=float(40)
base=float(10)
math.log(x, [base])
Traceback (most recent call last):
File "<string>", line 1, in <fragment>
TypeError: a float is required
math.log(x, [base]) doesn't literally mean "put base in brackets". This is what the documentation uses to denote an optional argument.
Remove them and it'll work
math.log(x, base)
Also, you don't need to use the float builtin to declare floats. Just add on a decimal component to your number and it will become a float:
x = 40.0
math.log(x, 10)
I have a large file with numbers in the form of 6,52353753563E-7. So there's an exponent in that string. float() dies on this.
While I could write custom code to pre-process the string into something float() can eat, I'm looking for the pythonic way of converting these into a float (something like a format string passed somewhere). I must say I'm surprised float() can't handle strings with such an exponent, this is pretty common stuff.
I'm using python 2.6, but 3.1 is an option if need be.
Nothing to do with exponent. Problem is comma instead of decimal point.
>>> float("6,52353753563E-7")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): 6,52353753563E-7
>>> float("6.52353753563E-7")
6.5235375356299998e-07
For a general approach, see locale.atof()
Your problem is not in the exponent but in the comma.
with python 3.1:
>>> a = "6.52353753563E-7"
>>> float(a)
6.52353753563e-07
I am running a code to select chunks from a big file. I am getting some strange error that is
"Invalid literal for float(): E-135"
Does anybody know how to fix this? Thanks in advance.
Actually this is the statement that is giving me error
float (line_temp[line(line_temp)-1])
This statement produces error
line_temp is a string
'line' is any line in an open and file also a string.
You need a number in front of the E to make it a valid string representation of a float number
>>> float('1E-135')
1e-135
>>> float('E-135')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for float(): E-135
In fact, which number is E-135 supposed to represent? 1x10^-135?
Valid literal forms for floats are here.
Looks like you are trying to convert a string to a float. If the string is E-135, then it is indeed an invalid value to be converted to a float. Perhaps you are chopping off a digit in the beginning of the string and it really ought to be something like 1E-135? That would be a valid float.
May I suggest you replace
float(x-y)
with
float(x) - float(y)
Ronald, kindly check the answers again. They are right.
What you are doing is: float(EXPRESSION), where the result of EXPRESSION is E-135. E-135 is not valid input into the float() function. I have no idea what the "line_temp[line(line_temp)-1]" does, but it returns incorrect data for the float() function.