| | |
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:
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: 106
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 ansi anti approximation assignment avogadro backend beginner binary bluetooth calculator character cmd code customdialog data decimals dictionaries dictionary directory drive dynamic error examples exe file float format function gnu graphics gui heads homework http ideas import input java launcher leftmouse line linux list lists logging loop module mouse number numbers output parsing path pointer port prime programming progressbar projects push py2exe pygame pyglet pyqt python random recursion schedule script scrolledtext sqlite statistics stdout string strings sudokusolver sum table terminal text thread threading time tlapse tricks tuple tutorial twoup ubuntu unicode urllib urllib2 variable ventrilo wikipedia windows write wxpython xlib






