| | |
Read a line from a txt file
Thread Solved |
•
•
Join Date: Sep 2009
Posts: 18
Reputation:
Solved Threads: 0
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
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)
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!
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!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
•
•
Join Date: Sep 2009
Posts: 18
Reputation:
Solved Threads: 0
Hi
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line
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
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line
python Syntax (Toggle Plain Text)
lpa=1 text_file = open("English101.txt", "r") print text_file.readline()[lpa] text_file.close()
•
•
Join Date: Oct 2007
Posts: 149
Reputation:
Solved Threads: 38
•
•
•
•
Hi
Thanks folks for your replies.
Testing the general format shown below returns a character and not the complete line
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 instructionpython Syntax (Toggle Plain Text)
lpa=1 text_file = open("English101.txt", "r") print text_file.readline()[lpa] text_file.close()
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)
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]
•
•
Join Date: Sep 2009
Posts: 100
Reputation:
Solved Threads: 11
I think you have to use the seek() function to set the offset in your file, try this maybe.
Python Syntax (Toggle Plain Text)
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
![]() |
Similar Threads
- Read contents of a .txt file (C)
- Replace Line in .txt File (C++)
- Help-Read line from the text file (Java)
- read line of text from file into array (C++)
- Read a particular line from a text file (C)
- Extract Words from a line in a .txt file (C++)
- how to move to the second line in C++ .txt file reading? (C++)
Other Threads in the Python Forum
- Previous Thread: Cookies
- Next Thread: Python loop help
| Thread Tools | Search this Thread |
abrupt accessdenied anti apache application approximation argv array beginner book builtin calculator change converter countpasswordentry curved dan08 dictionaries dictionary dynamic edit enter examples file float format function gui homework import inches input java keyboard lapse launcher library line lines linux list lists loop microphone mouse movingimageswithpygame mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple software sprite statictext string strings syntax table terminal text textarea thread threading time tlapse trick tuple tutorial twoup ubuntu unicode unit urllib urllib2 variable wordgame wxpython






