From 598af0df4a56b9fbbb6958aa7d695f6a19848a04 Mon Sep 17 00:00:00 2001 From: Jan Vlcinsky Date: Fri, 13 Apr 2018 22:33:53 +0200 Subject: [PATCH] Fix #78: printing resulting bytes under python3 Binary outputs were printed as repr with b'abcde' instead as real bytes. --- records.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/records.py b/records.py index 8d40822..da013be 100644 --- a/records.py +++ b/records.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- import os +from sys import stdout from collections import OrderedDict from contextlib import contextmanager from inspect import isclass @@ -523,7 +524,11 @@ Notes: # Print results in desired format. if format: - print(rows.export(format)) + content = rows.export(format) + if isinstance(content, bytes): + print_bytes(content) + else: + print(content) else: print(rows.dataset) except ImportError as impexc: @@ -532,6 +537,14 @@ Notes: print("Try to install missing packages.") exit(60) + +def print_bytes(content): + try: + stdout.buffer.write(content) + except AttributeError: + stdout.write(content) + + # Run the CLI when executed directly. if __name__ == '__main__': cli()