473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

question re file as sequence

I have a file with contents like:

Vegetable:
spinach
Fruit:
banana
Flower:
Daisy
Fruit:
pear

I want to print out the names of all the fruits. Is the following valid?

f = open(filename)
for line in f:
if line == 'Fruit:\n':
print f.readline()

The issue here is that the sequence has been mutated inside the loop.

If the above isn't good, anyone have a favorite alternative to doing
it like that?

Thanks.
Jul 18 '05 #1
4 1546
On 10 Jul 2004, it was written:
I want to print out the names of all the fruits. Is the following valid?

f = open(filename)
for line in f:
if line == 'Fruit:\n':
print f.readline()

The issue here is that the sequence has been mutated inside the loop.
Not precisely -- 'for line in f' uses f as an iterator, not a sequence: it
asks for (and gets) one line at a time from f, rather than slurping the
whole thing in as a sequence and looping over it (like f.readlines() would
do).

Unfortunately, when f is used as an iterator, it seems to move the file
pointer to the end of the file (question for the higher ups: why? StringIO
doesn't seem to do this). That's why f.readline() is returning ''. To
get around this, you'll have to use one or the other method exclusively.
Sticking with readline will probably be easiest:

f = open(filename)
for line in iter(f.readline,''):
if line == 'Fruit:\n':
print f.readline()

The iter() magic creates an iterator that returns one value at a time
using readline(), and stops when readline() returns ''.
If the above isn't good, anyone have a favorite alternative to doing
it like that?


If you're going to want to do lots of processing with the data (say
accessing Flowers after you work with Fruits) then I'd suggest massaging
the entire file into a dictionary, but that's probably overkill for what
you're trying to accomplish.

You may also want to use .strip() and possibly .lcase() on the input
strings and compare them against 'fruit:' in order to handle slightly
malformed data, but that may not be necessary if you know your data will
be consistent.

Jul 18 '05 #2
Christopher T King wrote:
Sticking with readline will probably be easiest:

f = open(filename)
for line in iter(f.readline,''):
if line == 'Fruit:\n':
print f.readline()


The (almost identical) solution using iterators is:

f = open(filename)
for line in f:
if line == 'Fruit:\n':
print f.next()

Of course, in this case you have to make sure a new line exists, or
handle the corresponding StopIteration exception.

--
Ciao,
Matteo
Jul 18 '05 #3
Christopher T King wrote:
Unfortunately, when f is used as an iterator, it seems to move the file
pointer to the end of the file (question for the higher ups: why? StringIO


From the documentation of file.next():

"""In order to make a for loop the most efficient way of looping over the
lines of a file (a very common operation), the next() method uses a hidden
read-ahead buffer. As a consequence of using a read-ahead buffer, combining
next() with other file methods (like readline()) does not work right.
However, using seek() to reposition the file to an absolute position will
flush the read-ahead buffer."""

http://docs.python.org/lib/bltin-file-objects.html

However, you cannot use tell() to find the absolute position, as it returns
the position at the end of the read-ahead buffer - tell() is just a thin
wrapper around the underlying C file. The easiest workaround becomes then

def readline(fileIter):
try:
return fileIter.next()
except StopIteration:
return ""
Peter

Jul 18 '05 #4
Hi Paul,

Here is how I would do this:
-----------------------------------

import string
counter = 0

f = open("c:\produce.txt", "r") # Obtain info from produce.txt file.
produce = f.readlines() # Store each line of text in a list called
produce.

while counter < len(produce):
if string.strip(produce[counter]) == "Fruit:": # Evaluate each line
in produce for a match.
print produce[counter + 1] # If matches, print next line.
counter = counter + 1 # Proceed to next item in text file.

-----------------------------------
However, there would be a much simplier way of doing this by changing
the data source slightly. See example:

Vegetable:spinach
Fruit:banana
Flower:Daisy
Fruit:pear
Here's how to parse this information and to find the fruits:

import string
f = open("c:\produce.txt", "r") # Open the file for reading.

category = []
for product in produce:
product = string.strip(product) # Remove the carriage return at
end of line.
category = category + [string.split(product, ":")] # Create a list [
[category, product], [category, product], [category, product] ] format.

# Display the items from the list.
for item in category:
if item[0] == "Fruit": # If category is "Fruit"
print item[1] # then display product from list.
-----------------------------------

Hope this helps!

Byron
---

Paul Rubin wrote:
I have a file with contents like:

Vegetable:
spinach
Fruit:
banana
Flower:
Daisy
Fruit:
pear

I want to print out the names of all the fruits. Is the following valid?

f = open(filename)
for line in f:
if line == 'Fruit:\n':
print f.readline()

The issue here is that the sequence has been mutated inside the loop.

If the above isn't good, anyone have a favorite alternative to doing
it like that?

Thanks.

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: wolfgang haefelinger | last post by:
Hi, I wonder whether someone could explain me a bit what's going on here: import sys # I'm running Mandrake 1o and Windows XP. print sys.version ## 2.3.3 (#2, Feb 17 2004, 11:45:40)
3
by: Cat | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I'm having trouble getting my head around the reason the that following file won't validate with the following error output in Xerces version...
1
by: Christos Kalantzis | last post by:
Hello, DB2 7.2 on AIX. I take online backups EVERY night and rsync my log folder every hour after running the DB2 ARCHIVE LOG command to make sure I dump the log buffer to a log file. Now...
9
by: noone | last post by:
I have a database file that I use an autonumber field as the primary key index. Because of some rearrangements in the past, this index does not match the order that I would like it to be in, that...
8
by: der | last post by:
Hello all, I've a question about order of evaluations in expressions that have && and || operators in them. The question is: will the evalution go left-to-right, no matter what -- even if the...
0
by: robert | last post by:
Hi all, I'm having a hard time resolving a namespace issue in my wsdl. Here's an element that explains my question, with the full wsdl below: <definitions name="MaragatoService"...
2
by: xin1wang1 | last post by:
I have just checked the faq and some refs on my bookshelf, but fail to find an answer to my following question: I am developing a code generator which generates c++ code from some interface...
0
by: rautsmita | last post by:
hello friends , i am using to jdk6 and JAXB2.0, i have geomtry.xsd file i am trying to compile this file using jaxb but i got some error i.e.The particle of the type is not a valid restriction of...
2
by: robtyketto | last post by:
Greetings, Within my jsp I have HTML code (see below) which accepts input, one of these fields sequence unlike the others is an Integer. <FORM ACTION="wk465682AddFAQ.jsp" METHOD="POST"> Id:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.