0

我正在使用 jsonstreams.Stream 方法使用 Python 2.7 将数据输入到 json 文件中,但是在尝试使用 readline() 方法从 PIPE 输入中读取数据时出现错误。该错误指出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])

我希望 output_log.json 的内容看起来像这样:

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

1 回答 1

0

Popen.communicate()返回 的元组(stdout_data, stderr_data)。见https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate

假设您想从stdout_data尝试替换行中读取

stdout_value = proc.communicate()

stdout_value, stderr_value = proc.communicate()
于 2021-01-02T21:22:35.517 回答