<div dir="ltr">Thanks Tim. That rstrip('\r\n') worked. :)<br><br><div class="gmail_quote">On Wed, Sep 17, 2008 at 10:45 AM, Tim Chase <span dir="ltr"><<a href="mailto:python.list@tim.thechases.com">python.list@tim.thechases.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="Ih2E3d"><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Is there any function for reading a file while ignoring *\n* occuring in the file?<br>
</blockquote>
<br>
can you be a bit more precise?  are we talking about text files or binary files?  how do you want to treat any newlines that actually appear in the file?<br>
</blockquote>
<br></div>
I believe the OP is referencing this behavior:<br>
<br>
  for line in file('x.txt'):<br>
    assert not (<br>
      line.endswith('\r') or<br>
      line.endswith('\n')), "Don't want this"<br>
  else:<br>
    yay()  # we never end up reaching this<br>
<br>
which can be done with a simple generator/wrapper:<br>
<br>
  def unnewline(iterator):<br>
    for line in iterator:<br>
      yield line.rstrip('\r\n')<br>
  for line in unnewline(file('x.txt')):<br>
    assert not (<br>
      line.endswith('\r') or<br>
      line.endswith('\n')), "Don't want this"<br>
  else:<br>
    yay()  #  we get here this time<br>
<br>
Alternatively, the content can just be modified on the fly:<br>
<br>
  for line in file('x.txt'):<br>
    line = line.rstrip('\r\n')<br>
    ...<br>
<br>
yes, the interpretation would differ if it were a binary file, but the above interpretation is a pretty common case (i.e. I encounter it daily, in my processing of client data-feeds).<br>
<br>
-tkc<div><div></div><div class="Wj3C7c"><br>
<br>
<br>
--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br></div>