pythonpython-2.7attributeerrorreadlinesjsonstream

readline() method is giving error: AttributeError: 'tuple' object has no attribute 'readline'


I am using jsonstreams.Stream method to input data into a json file using Python 2.7 but I am getting an error when trying to read data from the PIPE input using the readline() method. The error states that AttributeError: 'tuple' object has no attribute 'readline'.

branch_name = 'man/ashish_c/robotics'
days = '60'
json_output = 'output_log.json'

proc = subprocess.Popen(['git', 'log', branch_name, '--since="60 days ago"', '--pretty=%p\t%h\t%an\t%s\t%b'], shell=True, stdout=subprocess.PIPE)
stdout_value = proc.communicate()

with jsonstreams.Stream(jsonstreams.Type.array, filename=json_output) as output:
    while True:
        line = stdout_value.readline()
        if not line:
            break
        record = line.decode("utf-8").split("\t")
        with output.subobject() as output_e:
            output_e.write('merge', record[0])
            output_e.write('commit', record[1])
            output_e.write('author', record[2])
            output_e.write('title', record[3])
            output_e.write('body', record[4])

I want the contents of the output_log.json to look something like this:

[
    {"commit": "3011203d", "merge": "e84d9feb", "author": "me", "title": "test commit", "body": "..."},
    {"commit": "5033203d", "merge": "u67d9feb", "author": "Dmitri", "title": "check infra", "body": ".."}
    .....
]

Solution

  • Popen.communicate() returns a tuple of (stdout_data, stderr_data). See https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate

    Assuming you want to read from stdout_data try replacing the line

    stdout_value = proc.communicate()
    

    with

    stdout_value, stderr_value = proc.communicate()