Python
  Home arrow Python arrow Page 4 - Python 101 (part 5): Snake Oil For The...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PYTHON

Python 101 (part 5): Snake Oil For The Soul
By: Vikram Vaswani, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 4
    2001-07-09

    Table of Contents:
  • Python 101 (part 5): Snake Oil For The Soul
  • Air In A Bottle
  • Treading The Right Path
  • Standing In Line
  • Learning To Write
  • Desperately Seeking Python
  • String Theory
  • Pop() Goes The Weasel

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Python 101 (part 5): Snake Oil For The Soul - Standing In Line


    (Page 4 of 8 )

    There's also another method of reading data from a file - the readline()method call, used to read a single line at a time.

    >>> data = open("snakeoil.txt") >>> data.readline() 'After years of research, scientists at AB Labs have come up with a radical new product, once that promises to improve the quality of life for humans and animals everywhere.\012' >>> data.readline() ' \012' >>> data.readline() 'We call it Air In A Bottle(tm), and we firmly consider it to be one of our best inventions ever.\012' >>>
    You can combine this with a loop that will run through the file, printingone line after another:

    #!/usr/bin/python # open file data = open("snakeoil.txt") # read first line line = data.readline() # loop until EOF while line != "": print line, line = data.readline() else: print "-- all done --" # clean up data.close()
    Well, it works - but how about making it a little more efficient? Insteadof reading a file line by line, you can also suck the entire thing straightinto your program via a list - definitely more likely to impress the girls!

    #!/usr/bin/python # open file data = open("snakeoil.txt") # read file into list lines = data.readlines() # loop until EOF for x in lines: print x, else: print "-- all done --" # clean up data.close()
    In this case, the readlines() method is used to read the file into a list.Each element of the list now corresponds to a single line from the file.

    >>> data.readlines() ['After years of research, scientists at AB Labs have come up with a radical new product, once that promises to improve the quality of life for humans and animals everywhere.\012', ' \012', 'We call it Air In A Bottle(tm), and we firmly consider it to be one of our best inventions ever.\012', '\012', 'Carry it around with you (the bottle is titanium, the same material used in rocket ships, and the air is, well, air) and inhale from it whenever you need some air. Or give it to your friends as a present (after all, they need air too!)\012', '\012', 'Air In A Bottle(tm). Get some today!\012'] >>>
    Once this has been done, it's a simple matter to run through the list anddisplay its contents with the "for" loop.

    More Python Articles
    More By Vikram Vaswani, (c) Melonfire


     

       

    PYTHON ARTICLES

    - SSH with Twisted
    - Mobile Programming in Python using PyS60: UI...
    - Python: Count on It
    - Python Strings: Spinning Yarns
    - Python: More Fun with Strings
    - Python: Stringing You Along
    - Python Operators
    - Bluetooth Programming in Python: Network Pro...
    - Python Sets
    - Python Conditionals, Lists, Dictionaries, an...
    - Python: Input and Variables
    - Introduction to Python Programming
    - Mobile Programming in Python using PyS60: Ge...
    - Bluetooth Programming using Python
    - Finishing the PyMailGUI Client: User Help To...





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