Re: equivalent of perl $/

Guido.van.Rossum@cwi.nl
Mon, 16 Jan 1995 18:04:10 +0100

> Does python have an equivalent of perl's $/ variable? It'd be nice
> to be able to read data without \n being special...
>
> (In Perl, $/ is used as a delimiter for reading from files instead of \n.)

Gee, there's no end to the amount of $<magic> that Perl provides
that's actually useful :-) Let's see how we can simulate this in
Python... If you don't mind reading your file in all at once, try
this:

import string

file = open(...) # open the file
separator = '\0' # can be any string, possibly multiple characters
buffer = file.read()
records = string.splitfields(buffer, separator)

(Note that the separator won't be part of the records, unlike the list
returned by file.readlines() or the line returned by file.readline().)

--Guido van Rossum, CWI, Amsterdam <mailto:Guido.van.Rossum@cwi.nl>
<http://www.cwi.nl/cwi/people/Guido.van.Rossum.html>