By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,341 Members | 1,446 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,341 IT Pros & Developers. It's quick & easy.

xreadlines() being used with file.tell() and file.seek()

P: n/a

Hi all:

I am new to Python, and this is my first post (and it won't be my last!), so
HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an
inner loop in conjunction with "file.tell() and file.seek() in order to
navigate through a file in order to print specific lines (for example, every
5th line). Allow me to illustrate by example:

I have a file like this:

1:one

2:two

3:three

4:four

5:five

6:six

7:seven

8:eight

9:nine

10:ten

11:eleven

And I would like to use an outer and inner loop with the xreadlines()
command in order to provide this output:

the line I would like to print is 1

the line I would like to print is 6

the line I would like to print is 11

Now, I have code using "readline() " in both the inner and outer loop. This
code gives me the output that I am looking for:

------------------------------------------------------------------------

#!/usr/bin/env python

#opening the file for reading

str = 'myFile'

file = open(str, 'r')

#outer loop using file.readline

while 1:

line = file.readline()

if line == '':

break

data = line.rstrip().split(':')

print "the line I would like to print is", data [0]

#inner loop is reading contents of the file,

#and aftr 5 iterations,

# going back to outer loop

count = 0

while 1:

position = file.tell()

count = count +1

data = file.readline().strip().split(":")

if count == 5:

file.seek(position)

break

# end of inner while loop

#end of outer while loop

file.close()

--------------------------------------------------------------------

However, I have code using xreadlines() that does NOT work:

---------------------------------------------------------------------

#!/usr/bin/env python

# This section of code opens the file for reading

str = 'myFile'

file = open(str, 'r')

position = 0

# this outer loop uses xreadlines to read in all of the lines of the file

for line in file.xreadlines():

data = line.rstrip().split(':')

print "The line I would like to print is", data[0]

theFileSeek = file.seek(position)

#this iner loop reads through the file and loops through

#until count ==5. It then saves the position of the file

#when count ==5, and passes that info to the inner loop

count = 0

while 1:

position = file.tell()

line = file.readline()

data = line.rstrip().split(':')

count = count + 1

if count == 5:

file.seek(position)

break

#end while inner loop

#end while outer loop

file.close()

------------------------------------------------------------------

The code that does not work gives me this output:

The line I would like to print is 1

The line I would like to print is 2

The line I would like to print is 3

The line I would like to print is 4

The line I would like to print is 5

The line I would like to print is 6

The line I would like to print is 7

The line I would like to print is 8

The line I would like to print is 9

The line I would like to print is 10

The line I would like to print is 11

Please help!! How do I use the xreadlines() command (for the outer and inner
loop) to accomplish this? Why doesn't xreadlines() and file.seek() work
together? Or do they? I need to use xreadlines() instead of readline()
because the files I will be processing are huge, and xreadlines() processes
faster than readline(). Thanks, and sorry for the long post!
--
Remove ".nospam" from e-mail address to reply
Jul 18 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Pernell,

If I'm understanding your code correctly you need
to look closely at the .seek method. The argument
to this is bytes (not lines). Unless you have fixed
length lines, there is no "quick" way to go "seek" to
an arbitrary line in a file.

seek() and xreadlines do seem to work together:

from xreadlines import xreadlines

f=open(r"c:\readme.txt)
f.seek(1000) # Move down 1000 bytes
for line in xreadlines(f):
print l

This prints all the lines to the end of the file
beginning 1000 characters from the beginning of
the file.

Hope information helps.

Larry Bates
Syscon, Inc.
"Pernell Williams" <pe***********************@intel.com> wrote in message
news:c3**********@news01.intel.com...

Hi all:

I am new to Python, and this is my first post (and it won't be my last!), so HELLO EVERYONE!! I am attempting to use "xreadlines", an outer loop and an
inner loop in conjunction with "file.tell() and file.seek() in order to
navigate through a file in order to print specific lines (for example, every 5th line). Allow me to illustrate by example:

I have a file like this:

1:one

2:two

3:three

4:four

5:five

6:six

7:seven

8:eight

9:nine

10:ten

11:eleven

And I would like to use an outer and inner loop with the xreadlines()
command in order to provide this output:

the line I would like to print is 1

the line I would like to print is 6

the line I would like to print is 11

Now, I have code using "readline() " in both the inner and outer loop. This code gives me the output that I am looking for:

------------------------------------------------------------------------

#!/usr/bin/env python

#opening the file for reading

str = 'myFile'

file = open(str, 'r')

#outer loop using file.readline

while 1: line = file.readline() if line == '':
break
data = line.rstrip().split(':')
print "the line I would like to print is", data [0]
#inner loop is reading contents of the file,
#and aftr 5 iterations,
# going back to outer loop

count = 0

while 1:

position = file.tell()

count = count +1

data = file.readline().strip().split(":")

if count == 5:

file.seek(position)

break

# end of inner while loop

#end of outer while loop

file.close()

--------------------------------------------------------------------

However, I have code using xreadlines() that does NOT work:

---------------------------------------------------------------------

#!/usr/bin/env python

# This section of code opens the file for reading

str = 'myFile'

file = open(str, 'r')

position = 0

# this outer loop uses xreadlines to read in all of the lines of the file

for line in file.xreadlines():

data = line.rstrip().split(':')

print "The line I would like to print is", data[0]

theFileSeek = file.seek(position)

#this iner loop reads through the file and loops through

#until count ==5. It then saves the position of the file

#when count ==5, and passes that info to the inner loop

count = 0

while 1:

position = file.tell()

line = file.readline()

data = line.rstrip().split(':')

count = count + 1

if count == 5:

file.seek(position)

break

#end while inner loop

#end while outer loop

file.close()

------------------------------------------------------------------

The code that does not work gives me this output:

The line I would like to print is 1

The line I would like to print is 2

The line I would like to print is 3

The line I would like to print is 4

The line I would like to print is 5

The line I would like to print is 6

The line I would like to print is 7

The line I would like to print is 8

The line I would like to print is 9

The line I would like to print is 10

The line I would like to print is 11

Please help!! How do I use the xreadlines() command (for the outer and inner loop) to accomplish this? Why doesn't xreadlines() and file.seek() work
together? Or do they? I need to use xreadlines() instead of readline()
because the files I will be processing are huge, and xreadlines() processes faster than readline(). Thanks, and sorry for the long post!
--
Remove ".nospam" from e-mail address to reply

Jul 18 '05 #2

P: n/a
xreadlines() internally does something like this [using the Python 2.3
generator function syntax]:
def xreadlines(f):
while 1:
chunk = f.readlines(sizehint)
if not chunk: break
for line in chunk:
yield line

Unless you happen to seek() just as xreadlines is between chunks, the
result of the seek won't be seen until later, when the next chunk is
read. Your input file is far too small to notice this effect.

Similarly, after you've started an xreadlines() on a file, the file will
tell() you that it is after the end of all the lines in chunk.

xreadlines is supposed to be fast, and it really doesn't care what other
file-like object assumptions it invalidates to get that result.

Jeff

Jul 18 '05 #3

P: n/a
maybe you could re-post your code with standard indenting of 4 or 8
characters. I have trouble telling where I am with the single-character
indenting you are using.

but, a few questions:

Do you need to read in the entire file, or just certain lines?
Do you always know the pattern in advance? i.e. will it always be every 5th
line? If so, you may not need two loops.

If you want to read every line but only split out every nth line, you could
use integer div operator so that when you get to line no. count / n = 0,
you print .

cheers
Stewart
Jul 18 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.