Read a line from a txt file

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 18
Reputation: chico2009 is an unknown quantity at this point 
Solved Threads: 0
chico2009 chico2009 is offline Offline
Newbie Poster

Read a line from a txt file

 
0
  #1
Sep 27th, 2009
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

  1. fin = open ("origDict.txt", "r")
  2. fout = open ("newDict.txt", "w")
  3. lif = 0 # Lines in original file
  4. lpp = 72 # Lines per page. Default value, look at user selection later
  5. lpa = 1 # Line pointer a
  6. lpb = lpp # Line pointer b. This is effectively the offset.
  7. pc = 1 # Pages completed. Incremented each time
  8.  
  9.  
  10. linelist = fin.readlines() # Reads the origDict.txt file as a list of lines
  11. lif = len(linelist)
  12. print lif
  13.  
  14. while lpa != (lpp * pc ): # Loop until 1st page is complete
  15. lineA = fin.readline[lpa].strip() # Read line indicated by pointer a and delete CR
  16. lineB = fin.readline[lpb] # Read line indicated by pointer b
  17. newString = "%s\t%s" % (lineA, lineB)
  18. fout.write (newString) # write string to newDict
  19. print newString
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: Read a line from a txt file

 
1
  #2
Sep 27th, 2009
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!
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 1,546
Reputation: Ene Uran has a spectacular aura about Ene Uran has a spectacular aura about 
Solved Threads: 173
Ene Uran's Avatar
Ene Uran Ene Uran is offline Offline
Posting Virtuoso

Re: Read a line from a txt file

 
0
  #3
Sep 27th, 2009
Try something like:
lineA = fin.readline()[lpa].strip()
drink her pretty
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: chico2009 is an unknown quantity at this point 
Solved Threads: 0
chico2009 chico2009 is offline Offline
Newbie Poster

Re: Read a line from a txt file

 
0
  #4
Sep 29th, 2009
Hi
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line
  1. lpa=1
  2. text_file = open("English101.txt", "r")
  3. print text_file.readline()[lpa]
  4. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 149
Reputation: jice is on a distinguished road 
Solved Threads: 38
jice jice is offline Offline
Junior Poster

Re: Read a line from a txt file

 
1
  #5
Sep 30th, 2009
Originally Posted by chico2009 View Post
Hi
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line
  1. lpa=1
  2. text_file = open("English101.txt", "r")
  3. print text_file.readline()[lpa]
  4. 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
  1. lpa=1
  2. text_file = open("English101.txt", "r")
  3. linelist=text_file.readlines() # I load the whole file in a list
  4. text_file.close()
  5. print linelist[lpa]
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: chico2009 is an unknown quantity at this point 
Solved Threads: 0
chico2009 chico2009 is offline Offline
Newbie Poster

Re: Read a line from a txt file

 
0
  #6
Sep 30th, 2009
Hi All
Thanks for your help, understand it now
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: Read a line from a txt file

 
0
  #7
Sep 30th, 2009
You are welcome
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 106
Reputation: ov3rcl0ck is an unknown quantity at this point 
Solved Threads: 11
ov3rcl0ck ov3rcl0ck is offline Offline
Junior Poster

Re: Read a line from a txt file

 
0
  #8
Sep 30th, 2009
I think you have to use the seek() function to set the offset in your file, try this maybe.
  1. fin = open ("origDict.txt", "r")
  2. fout = open ("newDict.txt", "w")
  3. lif = 0 # Lines in original file
  4. lpp = 72 # Lines per page. Default value, look at user selection later
  5. lpa = 1 # Line pointer a
  6. lpb = lpp # Line pointer b. This is effectively the offset.
  7. pc = 1 # Pages completed. Incremented each time
  8.  
  9.  
  10. linelist = fin.readlines() # Reads the origDict.txt file as a list of lines
  11. lif = len(linelist)
  12. print lif
  13.  
  14. while lpa != (lpp * pc ): # Loop until 1st page is complete
  15. fin.seek(lpa)
  16. lineA = fin.readline.strip() # Read line indicated by pointer a and delete CR
  17. fin.seek(lpb)
  18. lineB = fin.readline # Read line indicated by pointer b
  19. newString = "%s\t%s" % (lineA, lineB)
  20. fout.write (newString) # write string to newDict
  21. print newString
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC