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().s plit(':')
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(posit ion)
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().s plit(':')
print "The line I would like to print is", data[0]
theFileSeek = file.seek(posit ion)
#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().s plit(':')
count = count + 1
if count == 5:
file.seek(posit ion)
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 3 4605
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:\rea dme.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************ ***********@int el.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().s plit(':') 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(posit ion)
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().s plit(':')
print "The line I would like to print is", data[0]
theFileSeek = file.seek(posit ion) #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().s plit(':')
count = count + 1 if count == 5:
file.seek(posit ion)
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
xreadlines() internally does something like this [using the Python 2.3
generator function syntax]:
def xreadlines(f):
while 1:
chunk = f.readlines(siz ehint)
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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 this while experimenting, trying to figure out the file objects modes,
|
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
ABCDEFGHIJKLMNOPQ
|
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:
|
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:
| |
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 byte-strings instead of unicode strings.
For example, if a file foo.txt contains some text encoded in latin1:
>>> import codecs
>>> f = codecs.open('foo.txt', 'r', 'utf-8', 'replace')
>>>
|
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 by 6. I
mean, ptr+6. Another way to ask the same question is how do you move
to the 3rd record in a file (considering that the file is made up of
records).
I told the interviewer that we could do a seek to move to the
specific byte. At that...
|
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:
disk.h:31:2: invalid preprocessing directive #inlcude
In file included from disk.cpp:8:
****disk.h:89: error: 'ifstream' is used as a type, but is not defined
as a type.*****
|
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 log file.
Basically, I'm trying to tail a log file and send the contents
elsewhere in the script (here, I call it processor()). My first
iteration below works perfectly fine- as long as the log file itself
(logfile.log) keeps getting written...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |