Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
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  
Old April 12th, 2008, 10:38 PM
maboroshi maboroshi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 47 maboroshi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 26 m 27 sec
Reputation Power: 5
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

Reply With Quote
  #2  
Old April 13th, 2008, 02:40 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jun 2004
Location: The People's Republic of Berkeley
Posts: 1,038 Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level)Schol-R-LEA User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 3 Weeks 3 Days 8 h 32 m 33 sec
Reputation Power: 330
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 ShortUnderstanding the C/C++ Preprocessor
Taming PythonA 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.

Reply With Quote
  #3  
Old April 13th, 2008, 03:29 AM
Bacat's Avatar
Bacat Bacat is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Location: Houston, TX
Posts: 118 Bacat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 31 m 12 sec
Reputation Power: 5
Send a message via MSN to Bacat
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.

Reply With Quote
  #4  
Old April 15th, 2008, 12:45 PM
auguri auguri is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2007
Posts: 32 auguri User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 39 m 52 sec
Reputation Power: 2
Discard them?

Quote:
Originally Posted by maboroshi
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


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):
row = infile.readline()
while row:
#do stuff to the data entry in row #get next row row = infile.readline()

Reply With Quote
  #5  
Old April 15th, 2008, 03:00 PM
maboroshi maboroshi is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2003
Posts: 47 maboroshi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 h 26 m 27 sec
Reputation Power: 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.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Read a txt file except for the first two lines


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway