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 there, I have found a really good link on how to read a file in Python in various ways:

here

I hope this helps!

commented: thanks for the direction +1

Try something like:
lineA = fin.readline()[lpa].strip()

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]
commented: Thanks a very helpful comment +1

Hi All
Thanks for your help, understand it now

You are welcome :)

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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.