473,416 Members | 1,525 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,416 software developers and data experts.

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


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
3 4574
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Grumfish | last post by:
Is there a way to see the next character of an input file object without advancing the position in the file?
5
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
8
by: Peter Abel | last post by:
Hi all, I'm working under W2k with Python 2.2.2 (#37, Oct 14 2002, 17:02:34) on win32 I have a file *test_data.txt* with the following content: 0123456789 0123456789 abcdefghi...
2
by: Pernell Williams | last post by:
Hi all: Thank you for your responses. I have a more specific question about "file.seek() and file.readline()" versus "file.seek() and file.xreadlines". When I have the following code:
5
by: Richard | last post by:
Hi, Can anyone tell me what the difference is between for line in file.readlines( ): and for line in file:
3
by: Eric Brunel | last post by:
Hi all, I just found a problem in the xreadlines method/module when used with codecs.open: the codec specified in the open does not seem to be taken into account by xreadlines which also returns...
39
by: Arvind Varma Kalidindi | last post by:
Hi, I was asked this question in an interview recently. "How do you move to the 6th byte in a file?" ... My thinking would be to find the data types in the file, set a base pointer and advance it...
4
by: uche | last post by:
Hello, I am having a problem with my code in C++/C. I have struggled for a while and cannot find the solution to the problem... Here is the compiled..output: In file included from disk.cpp:8:...
2
by: tgiles | last post by:
Hi, All! I started back programming Python again after a hiatus of several years and run into a sticky problem that I can't seem to fix, regardless of how hard I try- it it starts with tailing a...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.