readline() 方法给出错误:AttributeError: 'tuple' object has no attribute 'readline'
Posted
技术标签:
【中文标题】readline() 方法给出错误:AttributeError: \'tuple\' object has no attribute \'readline\'【英文标题】:readline() method is giving error: AttributeError: 'tuple' object has no attribute 'readline'readline() 方法给出错误:AttributeError: 'tuple' object has no attribute 'readline' 【发布时间】:2021-04-09 03:44:03 【问题描述】:我正在使用 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": ".."
.....
]
【问题讨论】:
communicate()
返回一个 (stdout_data, stderr_data)
的元组。
【参考方案1】:
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()
【讨论】:
以上是关于readline() 方法给出错误:AttributeError: 'tuple' object has no attribute 'readline'的主要内容,如果未能解决你的问题,请参考以下文章