473,385 Members | 1,409 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

problem moving from char to integer

Group,

I'm trying to get a very basic cast from a string to an integer
working. Here is what I'm doing:

for i in url:
result[counter] = int(i)
counter += 1;

return result;

anything that I'm doing drastically wrong? Mac OS 10.4, MacPython
upgraded to 2.4. Thanks for any help,

Melih

Sep 26 '06 #1
5 2141
Melih Onvural wrote:
Group,

I'm trying to get a very basic cast from a string to an integer
working. Here is what I'm doing:

for i in url:
result[counter] = int(i)
counter += 1;

return result;

anything that I'm doing drastically wrong? Mac OS 10.4, MacPython
upgraded to 2.4. Thanks for any help,

Melih
In what way does it not work? Please post the entire code or at least the
result of your code.
Sep 26 '06 #2
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;

Thanks

Pontus Ekberg wrote:
Melih Onvural wrote:
Group,

I'm trying to get a very basic cast from a string to an integer
working. Here is what I'm doing:

for i in url:
result[counter] = int(i)
counter += 1;

return result;

anything that I'm doing drastically wrong? Mac OS 10.4, MacPython
upgraded to 2.4. Thanks for any help,

Melih

In what way does it not work? Please post the entire code or at least the
result of your code.
Sep 26 '06 #3
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;

Thanks
Says that you are trying to change the character 'i' into
an integer, which won't work. Put a print statement just
before your for loop and print out url. It doesn't just
contain numbers (as apparently you think it does).

You should also lose the semicolons, you are writing Python
now, not PHP ;-).

You might also want to write:

def strord(url):
return [int(i) for i in url]

Or just lose the function completely since it degrades to a
single list comprehension.

-Larry Bates
Sep 26 '06 #4

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

Sep 27 '06 #5
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/...34386239051706

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
Sep 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: gg | last post by:
I am facing some problems with following program. I am using aCC version 03.27 on HP-UX11. The command line I use to compile is - aCC -AA -I. TestTempMethods.C Can anybody pls suggest how to...
7
by: Tim Conner | last post by:
Hi, I am an ex-delphi programmer, and I having a real hard time with the following simple code (example ): Which is the equivalent to the following code ? var chars : PChar; sBack, s :...
2
by: James Radke | last post by:
Hello, I have a vb.net windows application that is calling an older DLL provided by a third party. They supplied a VB 6 application that, when run on my systemn successfully passes data to the...
2
by: Mad Scientist Jr | last post by:
i'm trying to read a file byte by byte (and later alter the data and write it to a 2nd file byte by byte) and running into a problem where it seems to keep reading the same byte over and over again...
1
by: graemeharnish | last post by:
Hello. I'm trying to mod an open source app called TinyERP and inherit from a parent object, and in essence change how _column is defined. I found sample code of: class...
6
by: Jeff North | last post by:
I'm using Microsoft SQL Server Management Studio Express 9.00.2047.00 and expriencing problems with setting referential integrity on a link table. The tables' schema is as follows:...
5
by: dav3 | last post by:
I am by no means an ultra slick programmer and my problem solving skills.. well they leave much to be desired. That being said I have been working on the following problem for the past few days and...
1
by: rsteph | last post by:
I bought a book to help me learn to use DirectX with windows programming. It's first trying to walk me through some basic windows programming and graphics before getting into DirectX. I'm trying to...
5
by: adarshyam | last post by:
Hi friends, I have an interesting problem in vb.net. And I am struggling to get a solution for this..m trying for the past 3days.. It’s to calculate moving average for the inputs given by the...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.