problem moving from char to integer
John Machin
sjmachin at lexicon.net
Tue Sep 26 20:05:18 EDT 2006
Melih Onvural wrote:
> This is the error message that I'm having a tough time interpreting:
> Traceback (most recent call last):
> File "pagerank.py", line 101, in ?
> main()
> File "pagerank.py", line 96, in main
> ch = strord(url)
> File "pagerank.py", line 81, in strord
> result[counter] = int(i);
> ValueError: invalid literal for int(): i
>
> and here is the full code:
>
> def strord(url):
> counter=0;
> for i in url:
> result[counter] = int(i);
> counter += 1;
>
> return result;
You are getting this error because you are trying to convert string to
integer, but this of course complains if it finds unexpected
non-digits.
int('9') -> 9
int('x') -> this error
You may be looking for the ord() function:
ord('9') -> 57
ord('x') -> 120
In any case, once you've sorted that out, your function will die in the
same statement, because "result" isn't defined. Python isn't awk :-)
Given the name of your function (strord), perhaps what you really want
is this:
def strord(any_string):
return [ord(x) for x in any_string]
but why you'd want that (unless you were trying to pretend that Python
is C), I'm having a little troubling imagining ...
Perhaps if you told us what you are really trying to do, we could help
you a little better.
HTH,
John
More information about the Python-list
mailing list