0

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
  • 1
    Note to answerers: OUTPUT_RECORD_SEPARATOR is what is output after a print; OUTPUT_FIELD_SEPARATOR is what is output between the arguments to print. Commented Apr 21, 2009 at 5:01

5 Answers 5

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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

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().

Comments

1

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.

5 Comments

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

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,

Comments

0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.