|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
Read a txt file except for the first two lines
How can I read the entire contents of a txt file except for the first two lines
I can't seem to figure it out Cheers Maboroshi |
|
#2
|
||||
|
||||
|
Well, unless you are working with a fixed line size - which is unlikely given that you said 'lines' rather than 'records' - you would probably have to read in the first two lines and discard them, then read the rest. Fortunately, there are a number of built-in methods for reading the whole file as a list of strings or a generator of strings.
If you expect that the file will all fit into memory, the easiest way is probably to use readlines() to get the whole file as a list of strings, pop off the first two and then just treat it as a list. You could also make two calls to readline() (not the absence of the 's' at the end) to dispose of the first two lines, then use something like for line in file-object: to iterate through it. References http://docs.python.org/lib/bltin-file-objects.html http://www.penzilla.net/tutorials/python/fileio/ http://www.faqs.org/docs/diveintopy...info_files.html http://www.network-theory.co.uk/doc...ileObjects.html
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF #define KINSEY (rand() % 7) λ Scheme is the Red Pill Scheme in Short • Understanding the C/C++ Preprocessor Taming Python • A Highly Opinionated Review of Programming Languages for the Novice, v1.1 FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov Last edited by Schol-R-LEA : April 13th, 2008 at 02:54 AM. |
|
#3
|
||||
|
||||
|
Otherwise, if you know exactly how long the lines are, you can use the seek() method to go the correct place in the file. For example:
Code:
f = open('somefile.txt', 'r')
f.seek(position)
buffer = f.readlines()
But to be honest, I almost always just use the readlines() function and discard the lines I'm not interested in. Good luck!
__________________
Avalokiteshvara Bodhisattva, when practicing deeply the Prajna Paramita, perceives that all five skhandas in their own being are empty and is saved from all suffering. |
|
#4
|
|||
|
|||
|
Discard them?
Quote:
Maybe you're trying to read line by line? For example, you want to read a data file and do something to each entry and the first few lines are metadata. To handle that I just read the unwanted lines in and don't do anything with them. Then my pointer is in the right spot and I can get to work. There are lots of ways to do what you're trying to do. Here's an example of what I'm suggesting: Code:
infile = open('stuff.txt','r')
#Eat first two rows
for i in range(3):
|
|
#5
|
|||
|
|||
|
hmm
Well I figured out a quick solution
since I am using Tkinter and it is for records stored in a flat file system I just ran this command textbox.delete(2.0, 0.0) and seems to work great Last edited by maboroshi : April 15th, 2008 at 03:04 PM. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > Read a txt file except for the first two lines |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|