Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20528 > unrolled thread

Re: Undoing character read from file

Started byMRAB <python@mrabarnett.plus.com>
First post2012-02-16 23:31 +0000
Last post2012-02-17 22:58 -0500
Articles 3 — 3 participants

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: Undoing character read from file MRAB <python@mrabarnett.plus.com> - 2012-02-16 23:31 +0000
    Re: Undoing character read from file Neil Cerutti <neilc@norwich.edu> - 2012-02-17 13:12 +0000
      Re: Undoing character read from file Dave Angel <d@davea.name> - 2012-02-17 22:58 -0500

#20528 — Re: Undoing character read from file

FromMRAB <python@mrabarnett.plus.com>
Date2012-02-16 23:31 +0000
SubjectRe: Undoing character read from file
Message-ID<mailman.5904.1329435244.27778.python-list@python.org>
On 16/02/2012 23:10, Emeka wrote:
> Hello All,
>
> I know about seek and tell while using readline. What about if I am
> using read, and I want to undo the last character I just read(to return
> it back to the stream). How do I achieve this?
>
Try:

     f.seek(-1, 1)

It seeks -1 relative to the current position (the second argument
defaults to 0 for relative to start of file).

[toc] | [next] | [standalone]


#20552

FromNeil Cerutti <neilc@norwich.edu>
Date2012-02-17 13:12 +0000
Message-ID<9q7217F6j0U3@mid.individual.net>
In reply to#20528
On 2012-02-16, MRAB <python@mrabarnett.plus.com> wrote:
> On 16/02/2012 23:10, Emeka wrote:
>> Hello All,
>>
>> I know about seek and tell while using readline. What about if I am
>> using read, and I want to undo the last character I just read(to return
>> it back to the stream). How do I achieve this?
>>
> Try:
>
>      f.seek(-1, 1)
>
> It seeks -1 relative to the current position (the second
> argument defaults to 0 for relative to start of file).

Unless it's a stream opened in binary mode this will not work.
You'd need to maintain a n-character length buffer instead, with
n being the maximum number of characters you'd like to be able to
put back.

-- 
Neil Cerutti

[toc] | [prev] | [next] | [standalone]


#20579

FromDave Angel <d@davea.name>
Date2012-02-17 22:58 -0500
Message-ID<mailman.5933.1329537548.27778.python-list@python.org>
In reply to#20552
On 02/17/2012 10:38 PM, Emeka wrote:
> Hello All,
>
> Say I have something like this:
>
> mfile = open("cc.txt", "rb")
> mcount = 0
> mset = False
> while True:
> c = mfile.read(1)
> if c == "e" and mset is True and mcount == 0:
>      print c
>      mfile.seek(-1,1)
>      mcount = 1
>      continue
> elif c == "e" and mset is False and mcount == 0:
>      print c
>      mfile.seek(-1, 0)
>      mcount = 1
>      continue
> elif c == "e" and mcount == 1:
>      print c
>      mcount = 0
>      continue
> print c
>   if mset is False:
>      mset = True
> if len(c) == 0:
>      break
>
> cc.txt
>
> foor the this the been we hate to sh wiukr bee here today. But who are we
> to question
> him concerning this issue.
>
> Is the above code the right way?

You top-posted, instead of putting your response after whatever you were 
quoting.  So you lose all context.

Your code won't compile, and it's unclear just what you were trying to 
accomplish.  What do you mean by "the right way"?

Please post the actual code that you're running, and explain what you 
expected, what you got, and how it didn't do what you wanted.  In this 
case, you should give us the traceback, so it's obvious that you're 
trying to figure out how to indent.



-- 

DaveA

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web