943,882 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 2643
  • Python RSS
Sep 27th, 2009
0

Read a line from a txt file

Expand Post »
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

python Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chico2009 is offline Offline
18 posts
since Sep 2009
Sep 27th, 2009
1

Re: Read a line from a txt file

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!
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Sep 27th, 2009
0

Re: Read a line from a txt file

Try something like:
lineA = fin.readline()[lpa].strip()
Reputation Points: 625
Solved Threads: 211
Posting Virtuoso
Ene Uran is offline Offline
1,704 posts
since Aug 2005
Sep 29th, 2009
0

Re: Read a line from a txt file

Hi
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line
python Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chico2009 is offline Offline
18 posts
since Sep 2009
Sep 30th, 2009
1

Re: Read a line from a txt file

Click to Expand / Collapse  Quote originally posted by chico2009 ...
Hi
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line
python Syntax (Toggle Plain Text)
  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
python Syntax (Toggle Plain Text)
  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]
Reputation Points: 64
Solved Threads: 56
Posting Whiz in Training
jice is offline Offline
225 posts
since Oct 2007
Sep 30th, 2009
0

Re: Read a line from a txt file

Hi All
Thanks for your help, understand it now
Reputation Points: 10
Solved Threads: 0
Newbie Poster
chico2009 is offline Offline
18 posts
since Sep 2009
Sep 30th, 2009
0

Re: Read a line from a txt file

You are welcome
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Sep 30th, 2009
0

Re: Read a line from a txt file

I think you have to use the seek() function to set the offset in your file, try this maybe.
Python Syntax (Toggle Plain Text)
  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
Reputation Points: 35
Solved Threads: 22
Junior Poster
ov3rcl0ck is offline Offline
113 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Cookies
Next Thread in Python Forum Timeline: Python loop help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC