problem moving from char to integer

Melih Onvural melih.onvural at gmail.com
Wed Sep 27 00:21:36 EDT 2006


I'm trying to calculate the checksum of a website so that I can get its
PageRank. I'm modifying the code that I found here:
http://blog.outer-court.com/archive/2004_06_27_index.html#108834386239051706

I apologize for being secretive, but I didn't mean to be. I'm trying to
take characters and push them to their integer value. Thanks for the
help and the advice on writing in Python. Definitely learning to do
things right is the goal here.

Also, the ord function worked. Thanks.

Melih

John Machin wrote:
> 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