Is there a way to see the next character of an input file object without
advancing the position in the file? 7 4926
On Sun, 27 Jul 2003 01:09:54 +0000, Grumfish wrote: Is there a way to see the next character of an input file object without advancing the position in the file?
To do this, you can do the following:
fin = file('myfile')
....
char = fin.read(1)
fin.seek(-1,1) # set the file's current position back a character
You can then write your own subclass of file, if you want, with "peek"
functionality:
class flexfile(file):
def __init__(self, fname, mode='r', bufsize=0):
file.__init__(s elf, fname, mode, bufsize)
def peek(self, cnt):
data = self.read(cnt)
self.seek(cnt * -1, 1)
return data
def peekline(self):
pos = self.tell()
data = self.readline()
self.seek(pos, 0)
return data
On Sun, 27 Jul 2003 02:58:13 GMT, "Keith Jones" <kj*****@roches ter.rr.com> wrote: On Sun, 27 Jul 2003 01:09:54 +0000, Grumfish wrote:
Is there a way to see the next character of an input file object without advancing the position in the file?
To do this, you can do the following:
fin = file('myfile')
...
char = fin.read(1) fin.seek(-1,1) # set the file's current position back a character
Warning, though: this is very iffy on windows unless you have opened the file
in binary. E.g., print >> file('ends_in_w indows_EOL.txt' ,'w'), 'Ends in windows EOL here:'
Look at it in binary: file('ends_in_w indows_EOL.txt' ,'rb').read()
'Ends in windows EOL here:\r\n'
Cooked: file('ends_in_w indows_EOL.txt' ,'r').read()
'Ends in windows EOL here:\n'
Now try to seek back past the apparent \n single character and one more (2)
so we can read the ':' f = file('ends_in_w indows_EOL.txt' ) f.seek(-2, 2) f.read()
'\n'
Hm. That's a representation of reading the last two characters in cooked mode.
Apparently the seek positioned us to read '\r\n', not a cooked ':\n'
Look at the same in binary: f = file('ends_in_w indows_EOL.txt' , 'rb') f.seek(-2, 2) f.read()
'\r\n'
The last two are the windows EOL. Seeking -2 in cooked mode is not positioning at ':\n'
as we can see in the binary:
f.seek(-3, 2) f.read()
':\r\n'
[...]
So if you're going to seek/tell, best to do it in binary, and deal with the platform dependent EOLs.
Regards,
Bengt Richter
[Keith] def peek(self, cnt): data = self.read(cnt) self.seek(cnt * -1, 1) return data
def peekline(self): pos = self.tell() data = self.readline() self.seek(pos, 0) return data
[Bengt] if you're going to seek/tell, best to do it in binary, and deal with the platform dependent EOLs.
seek() works perfectly with text-mode files as long as you only seek to
places given to you by tell(). So if Keith's peek() function had used
tell() and then seek()ed (sought()? 8-) back to that point like his
peekline() does, there would be no problem.
--
Richie Hindle ri****@entrian. com
Grumfish <no****@nowhere .com> wrote in message news:<CD******* ************@tw ister.nyroc.rr. com>... Is there a way to see the next character of an input file object without advancing the position in the file?
Here is a little class that I use on a little html parser that I
wrote. You may be able to adjust it for your needs:
class Characters:
def __init__(self, String):
self.Characters =String
self.Char = ""
self.index = 0
self.lenght = len(self.Charac ters)
def GetNextChar(sel f):
skipchar = 1
while skipchar ==1:
try:
self.Char = self.Characters[self.index]
except IndexError:
self.Char = None
#print "End of File\n"
return None
self.index += 1
if self.Char != "\n":
skipchar = 0
return self.Char
def CheckNextChar(s elf):
skipchar = 1
StartChar = self.Char
StartIndex = self.index
while skipchar ==1:
try:
self.Char = self.Characters[self.index]
except IndexError:
self.Char = None
#print "End of File\n"
return None
self.index += 1
if self.Char != "\n":
skipchar = 0
self.index = StartIndex
NextChar = self.Char
self.Char = StartChar
return NextChar
[Richie] seek() works perfectly with text-mode files as long as you only seek to places given to you by tell(). So if Keith's peek() function had used tell() and then seek()ed (sought()? 8-) back to that point like his peekline() does, there would be no problem.
[Bengt] Can you cite a C or C++ standard section that guarantees that seek/tell will work that way in text mode? (I'm not saying there isn't one, but I couldn't find one quickly ;-)
ANSI C, ISO/IEC 9899:1990, section 7.9.9.2: "For a text stream, either
offset shall be zero, or offset shall be a value returned by an earlier
call to the ftell function on the same stream and whence shall be
SEEK_SET."
--
Richie Hindle ri****@entrian. com
On Tue, 29 Jul 2003 09:05:53 +0100, Richie Hindle <ri****@entrian .com> wrote: [Richie] seek() works perfectly with text-mode files as long as you only seek to places given to you by tell(). So if Keith's peek() function had used tell() and then seek()ed (sought()? 8-) back to that point like his peekline() does, there would be no problem.
[Bengt] Can you cite a C or C++ standard section that guarantees that seek/tell will work that way in text mode? (I'm not saying there isn't one, but I couldn't find one quickly ;-)
ANSI C, ISO/IEC 9899:1990, section 7.9.9.2: "For a text stream, either offset shall be zero, or offset shall be a value returned by an earlier call to the ftell function on the same stream and whence shall be SEEK_SET."
Thank you. It makes sense that you could retrieve and set an underlying binary position
and decode forward with arbitrary cooking, so long as that cooking either goes character
by character and leaves the binary position to right after what it has used up, or does
the equivalent by proper binary repositioning if it grabs a chunk and doesn't use all of it
for a given cooking portion, so that the next retrieval (tell) will be valid.
I guess I must have been bitten some time in the foggy past, and never recovered trust.
Now I have enough at least to use it, though I will probably still want to write a little
verification into a unit test if I'm in finicky mode. Thanks again ;-)
BTW, is there an ANSI C spec available free on line (or an older, close-enough draft)?
Regards,
Bengt Richter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: les_ander |
last post by:
Hi,
suppose I am reading lines from a file or stdin.
I want to just "peek" in to the next line, and if it starts
with a special character I want to break out of a for loop,
other wise I want to do readline().
Is there a way to do this?
for example:
while 1:
line=stdin.peek_nextline()
|
by: Steven Bethard |
last post by:
This has probably been answered before, but my Google skills have failed
me so far...
Is there an os independent way of checking to see if a particular
executable is on the path? Basically what I want to do is run code like:
i, o, e = os.popen3(executable_name)
but I'd like to give an informative error if 'executable_name' doesn't
refer...
|
by: AndrewM |
last post by:
Hello,
I have a SUB which has input variables. If I run it once its okay, but I
need to include it in a for....next loop.
Can this be done ?
for i=0 to 100
call mySub(arr(0,i))
next
|
by: dev_kh |
last post by:
Hi,
I am facing a weird issue. Here it goes:
I am writing a c# app for a client with which I will read some data and
generate a .dat file for them from that data. Then the client will read that
..dat file and do something based on it's contents at their end. My
application will run on a windows xp machine but client should be able to...
|
by: seema_coma |
last post by:
I am working under a Linux environment, and I want to
"read stream up to next delimiter". It looks like linux
doesnt't have a function such as bgets()
All i need is,
reading characters from stream into buffer until either
count is exhausted or one of the characters in breakstring
is encountered in the stream. The read data is terminated...
| |
by: Jukka K. Korpela |
last post by:
I recently noticed, once again, how the common implementation of italics can
really disturb. I'm referring to the common phenomenon that there is by
default too little spacing after italics text, so that if you have, say,
<em>Bill</emWatterson
then the last "l" of "Bill" hits the "W" - they may even slighly overlap. I
noticed this long ago,...
|
by: ctrohana |
last post by:
Hi guys,
I need ur help.
If I have character strings (data file) as below:
X001234
X001345Y002323
X00142
X001567
How do i program, if I want to enter Y002323 to the next line? and how I can add '0' character after X00142 so that the strings are like:
|
by: GISmatters |
last post by:
I have unbound checkboxes in a nested gridview to allow multi-selection of "child" rows. For context, the parent gridview rows are for large "reports", the child rows are for various specific files comprising each report. I want the user to be able to select an arbitrary collection of report files and have them emailed by clicking an "Email...
|
by: tempest |
last post by:
Hi all.
This is a rather long posting but I have some questions concerning the
usage of character entities in XML documents and PCI security
compliance.
The company I work for is using a third party ecommerce service for
hosting its online store. A few months ago this third party commerce
site began using PGP file encryption on XML...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
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...
|
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...
|
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...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
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.
| |