tags:

views:

175

answers:

5

Does Python have an equivalent to the $OUTPUT_RECORD_SEPARATOR or $\ in Perl?

UPDATE: I totally had this wrong... I was looking for a Python equivalent to a $INPUT_RECORD_SEPARATOR if there is such a thing? Something that you can override so that when you do a readline() type call its looking for something other than the newline char. Sorry about botching the original question.

+1  A: 

You could use the ''.join method. e.g.

# print 'foo', 'bar', 'baz' separated by spaces
print 'foo', 'bar', 'baz'
# print separated by commas
print ', '.join(['foo', 'bar', 'baz'])

EDIT:

Ok, I misunderstood the purpose of OUTPUT_RECORD_SEPARATOR, so ''.join is not what you want.

print 'foo' is equivalent to sys.stdout.write('foo'+'\n'), so you could roll your own print function:

def myprint(*args, end='\n'):
    sys.stdout.write(' '.join(args) + end)

Or go one step further with a factory function:

def make_printer(end):
    def myprint(*args, end='\n'):
        sys.stdout.write(' '.join(args) + end)
    return myprint

# usage:
p = make_printer('#')
p('foo', 'bar', 'baz')

Finally, if you're daring, you could override sys.stdout:

sys.old_stdout = sys.stdout
class MyWrite(object):
    def __init__(self, end='\n'):
        self.end = end
    def write(self, s):
        sys.old_stdout.write(s.replace('\n', self.end))

This will cause print statements to produce the alternative line ending. Usage:

sys.stdout = MyWrite('!\n')
print 'foo'
# prints: foo!

Note that you may need to override more than just write() – or at least provide redirects for things like MyWrite.flush() to sys.old_stdout.flush().

John Fouhy
+1  A: 

Python 3's print function has sep and end arguments.

print('foo', 'bar', 'baz', sep='|', end='#')

Note that in Python 3, print is no longer a statement, it is a function.

codeape
Or in the case of OUTPUT_RECORD_SEPARATOR, print('foo', 'bar', 'baz', end='|')
Miles
Thanks, I edited the answer. Never did any Perl coding, so I just guessed about what OUTPUT_RECORD_SEPARATOR is.
codeape
A: 

In Python pre-3, you can suppress standard separator appending ',' at end of line:

print yourVar,  #these 3 lines print without any newline separator
print yourVar, 
print yourVar,

if you want to use non-standard separator, suppress normal one and print yours:

print yourVar, yourSep,
print yourVar, yourSep,
print yourVar, yourSep,
vartec
+1  A: 

If what you want is to have any of \r, \n, or \r\n in the input to be seen as a newline, then you can use “universal newline support” in your ‘open()’ call.

In Python 2.6 open(), this needs to be explicitly enabled by adding ‘U’ to the mode string:

in_file = open('foo.txt', 'rU')

It also depends on this support being available in the Python interpreter; but the docs say this is available by default.

In Python 3.0 open(), universal newline behaviour is always available, and is the default behaviour; you can choose a different behaviour with different values for the newline parameter.

If you want input to interpret records terminated by something other than a newline, then you don't want ‘readlines’ (which, per the name, reads lines specifically, not records generally) and AFAIK will have to implement your own record-reading code.

bignose
What if I want \x00 to be seen as the line end?
tyndall
Or is there another elegant method of drinking in a little of the file at a time delimited by something other than a newline?
tyndall
+1 for the Universal Newline thing. Didn't know about that. Thx.
tyndall
python is normally compiled with Universal new line support enabled, so adding 'U' usually makes no different.
SilentGhost
@SilentGhost: that's different between Python 2.6 and Python 3.0. I've updated the answer to cover this.
bignose
A: 
open('file').read().split(your_separator_of_choice)

the only difference with readlines() here is that resulting list items won't have separator preserved at the end.

SilentGhost