Converting from string to int in file from the internet - python

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]

Related

Python : Get count of successfully matched groups for regex

I want to capture data and numbers from a string in python. The string is a measurement from an RF sensor so it might be corrupted from bad transmission. Strings from the sensor look like this PA1015.7 TMPA20.53 HUM76.83.
My re is :
s= re.search('^(\D+)([0-9.]+'),message)
Now before I proceed I want to check if I truly received exactly two matches properly or if the string is garbled.
So I tried :
len(s)
But that errors out :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: object of type '_sre.SRE_Match' has no len()
I do need access to the match group elements for processing later. (I think that eliminates findall)
key= s.group(1)
data= s.group(2)
What's missing?
Instead of using search, you should use findall instead:
s = re.findall('(\D+)([0-9.]+)',message)
print("matched " + str(len(s)))
search only returns whether there is or is no match in the input string, in the form of a boolean.

Cant convert an object into an int

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

Python Index Error: string index out of range

## A little helper program that capitalizes the first letter of a word
def Cap (s):
s = s.upper()[0]+s[1:]
return s
Giving me this error :
Traceback (most recent call last):
File "\\prov-dc\students\jadewusi\crack2.py", line 447, in <module>
sys.exit(main(sys.argv[1:]))
File "\\prov-dc\students\jadewusi\crack2.py", line 398, in main
foundit = search_method_3("passwords.txt")
File "\\prov-dc\students\jadewusi\crack2.py", line 253, in search_method_3
ourguess_pass = Cap(ourguess_pass)
File "\\prov-dc\students\jadewusi\crack2.py", line 206, in Cap
s = s.upper()[0]+s[1:]
IndexError: string index out of range
As others have already noted, the problem is that you're trying to access an item in an empty string. Instead of adding special handling in your implementation, you can simply use capitalize:
'hello'.capitalize()
=> 'Hello'
''.capitalize()
=> ''
It blows up, presumably, because there is no indexing an empty string.
>>> ''[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
And as it has been pointed out, splitting a string to call str.upper() on a single letter can be supplanted by str.capitalize().
Additionally, if you should regularly encounter a situation where this would be passed an empty string, you can handle it a couple of ways:
…#whatever previous code comes before your function
if my_string:
Cap(my_string) #or str.capitalize, or…
if my_string being more or less like if len(my_string) > 0.
And there's always ye old try/except, though I think you'll want to consider ye olde refactor first:
#your previous code, leading us to here…
try:
Cap(my_string)
except IndexError:
pass
I wouldn't stay married to indexing a string to call str.upper() on a single character, but you may have a unique set of reasons for doing so. All things being equal, though, str.capitalize() performs the same function.
>>> s = 'macGregor'
>>> s.capitalize()
'Macgregor'
>>> s[:1].upper() + s[1:]
'MacGregor'
>>> s = ''
>>> s[0]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: string index out of range
>>> s[:1].upper() + s[1:]
''
Why does s[1:] not bail on an empty string?
Tutorial on strings says:
Degenerate slice indices are handled gracefully: an index that is too
large is replaced by the string size, an upper bound smaller than the
lower bound returns an empty string.
See also Python's slice notation.
I just had the same error while I was sure that my string wasn't empty. So I thought I'd share this here, so people who get that error have as many potentional reasons as possible.
In my case, I declared a one character string, and python apparently saw it as a char type. It worked when I added another character. I don't know why it doesn't convert it automatically, but this might be a reason that causes an "IndexError: string index out of range", even if you think that the supposed string is not empty.
It might differ between Python versions, I see the original question refers to Python 3. I used Python 2.6 when this happened.

ValueError: invalid literal for int() with base 10: '107.24'

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

python Invalid literal for float

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.

Categories

Resources