split text file with x0Dx0A

Fredrik Lundh fredrik at pythonware.com
Thu Dec 29 06:48:53 EST 2005


"ownowl" wrote:

> beginer under python, I have a problem to get lines in a text file.
> lines have inside the \n (x0A) char, and le readline method split the
> line at this char too (not only at x0Dx0A).

that's not a very clever design, at least if you plan to read the files
from C or compatible languages...

> for resume, I want to split a file to lines with only those chars : x0Dx0A
>
> A idea ?

the easiest way to do this is to open the file in binary mode, gobble up
the entire file, and split it yourself:

    for line in open(filename, "rb").read().split("\r\n"):
        ...

</F>






More information about the Python-list mailing list