Take the 2-minute tour ×

everyone: I run my python script functions like this: read from a text file, and store the data as dict. But when in the loop, an Illegal instruction occurs. why this happens? the code is like this:

datafile=open('a.txt') # a big text file
for line in datafile:
    for token in line.split():
        print("Parsing line %d." % token[0])

then the message is like this:

Parsing line 1.
Parsing line 2.
Parsing line 1064
Parsing line 1065
Illegal instruction

what's the problem? my platform is python 2.6.2 on AIX 5.2. please help me, thanks!

share|improve this question
What is printing "Illegal instruction"? What else is printed? –  John Machin Mar 19 '10 at 11:52

1 Answer 1

This looks very wrong. token is a string in an array of strings returned by line.split(). So token[0] is the first character of that string. Therefore I don't believe that you'll ever get anything like Parsing line 1065 in your output. As Mark wrote, you'd see a TypeError.

Please post

  1. the real code
  2. the real data (an excerpt of the first few lines and line 1065/1066 would suffice)
  3. the real error message.

Otherwise there's no way to help you.

share|improve this answer
+1 Formatting a string with "%d" should give a TypeError; it seems very unlikely that this code is producing the results described. –  Mark Dickinson Mar 19 '10 at 12:13
The second for loop appears to be completely unnecessary for what he's appearing to be trying to do. –  Tim Pietzcker Mar 19 '10 at 12:46

Your Answer


