Hi
I am having problems using the readline command, I get an alarm
lineA = fin.readline[lpa].strip() # Read line indicated by pointer a and delete CR
TypeError: 'builtin_function_or_method' object is unsubscriptable
when trying to run this code
I tried removing the strip component and that didnt help. Some help would be much appreciated

fin = open ("origDict.txt", "r")
fout = open ("newDict.txt", "w")
lif = 0 # Lines in original file
lpp = 72 # Lines per page. Default value, look at user selection later
lpa = 1 # Line pointer a
lpb = lpp # Line pointer b. This is effectively the offset.
pc  = 1 # Pages completed. Incremented each time


linelist = fin.readlines() # Reads the origDict.txt file as a list of lines
lif = len(linelist)
print lif

while lpa != (lpp * pc ): # Loop until 1st page is complete
    lineA = fin.readline[lpa].strip() # Read line indicated by pointer a and delete CR
    lineB = fin.readline[lpb] # Read line indicated by pointer b
    newString = "%s\t%s" % (lineA, lineB)
    fout.write (newString) # write string to newDict
    print newString

Hi
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line

lpa=1
text_file = open("English101.txt", "r")
print text_file.readline()[lpa]
text_file.close()

What I am trying to achieve is that a complete line of text is returned, from a text file that has 600 lines or so. Maybe I am looking at the wrong instruction

Hi
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line

lpa=1
text_file = open("English101.txt", "r")
print text_file.readline()[lpa]
text_file.close()

What I am trying to achieve is that a complete line of text is returned, from a text file that has 600 lines or so. Maybe I am looking at the wrong instruction

readline() reads the next line. So, readline()[nb] takes the nbth character of the next line.
You can't read the nth line of a file that way. You have to read line by line TILL the nth line or read the whole file in a list (like you did in linelist) and then

lpa=1
text_file = open("English101.txt", "r")
linelist=text_file.readlines() # I load the whole file in a list
text_file.close()
print linelist[lpa]
Comments
Thanks a very helpful comment

I think you have to use the seek() function to set the offset in your file, try this maybe.

fin = open ("origDict.txt", "r")
fout = open ("newDict.txt", "w")
lif = 0 # Lines in original file
lpp = 72 # Lines per page. Default value, look at user selection later
lpa = 1 # Line pointer a
lpb = lpp # Line pointer b. This is effectively the offset.
pc  = 1 # Pages completed. Incremented each time


linelist = fin.readlines() # Reads the origDict.txt file as a list of lines
lif = len(linelist)
print lif

while lpa != (lpp * pc ): # Loop until 1st page is complete
    fin.seek(lpa)
    lineA = fin.readline.strip() # Read line indicated by pointer a and delete CR
    fin.seek(lpb)
    lineB = fin.readline # Read line indicated by pointer b
    newString = "%s\t%s" % (lineA, lineB)
    fout.write (newString) # write string to newDict
    print newString
This question has already been answered. Start a new discussion instead.