1

Question:

Python3.6 is printing the literal character \n and \t in the output

    Output:
    ===============================
    $ python3.6 sample.py
    Running system command: hadoop fs -cat /user/mapr/rajesh/sample.txt
    b'1320352532\t1001\thttp://www.mapr.com/doc\thttp://www.mapr.com\t192.168.10.1\n1320352533\t1002\thttp://www.mapr.com\thttp://www.example.com\t192.168.10.10\n1320352546\t1001\thttp://www.mapr.com\thttp://www.mapr.com/doc\t192.168.10.1\n'
    b'1320352532\t1001\thttp://www.mapr.com/doc\thttp://www.mapr.com\t192.168.10.1\n'
    b'1320352533\t1002\thttp://www.mapr.com\thttp://www.example.com\t192.168.10.10\n'
    b'1320352546\t1001\thttp://www.mapr.com\thttp://www.mapr.com/doc\t192.168.10.1\n'

    Code:
    # To print the sample HDFS file
    cat sample.py

import sys
import subprocess

 cat = subprocess.Popen(["hadoop", "fs", "-cat", "/user/mapr/rajesh/sample.txt"], stdout=subprocess.PIPE)
    for line in cat.stdout:
        print (line)
Rajesh
  • 65
  • 6
  • 3
    I think it's because you have byte strings and not strings. `print(b'\t')` vs `print('\t')` – Buckeye14Guy Jun 26 '19 at 19:30
  • Is there a chance that it's actually hadoop that is outputting the newline and tab characters? I've seen similar SO questions around subprocess and 'echo' - it was echo that was outputting the literals, not Python. For example: https://stackoverflow.com/questions/36422572/python-subprocess-output-without-n – ingernet Jun 26 '19 at 19:37
  • use `print( line.decode() )` – furas Jun 26 '19 at 19:39
  • 1
    Possible duplicate of [subprocess popen stdout](https://stackoverflow.com/questions/33435110/subprocess-popen-stdout) – SyntaxVoid Jun 26 '19 at 19:41

0 Answers0