如何从 DetailedResponse 对象获取 "transcript" 数据?
How do I get "transcript" data from DetailedResponse object?
我使用 IBM 语音转文本服务从音频文件中获取文本。
这就是数据的样子
{
"result": {
"results": [
{
"alternatives": [
{
"confidence": 0.6,
"transcript": "state radio "
}
],
"final": true
},
{
"alternatives": [
{
"confidence": 0.77,
"transcript": "tomorrow I'm headed to mine nine
consecutive big con I'm finna old tomorrow I've got may meet and greet
with whoever's dumb enough to line up for that and then on Friday you can
catch me on a twitch panel"
}
],
"final": true
我试着用
把它变成 JSON
print(json.dumps(output, indent = 4))
但是报错
TypeError: Object of type DetailedResponse is not JSON serializable
如何使用此数据仅打印 "transcript"?
json.dumps()
将 Python 对象转换为 JSON 字符串,这是由 API 样本完成的,用于记录/打印响应,但有一个怪癖Python 3.7 是什么使 Python 对象 json 可序列化发生了变化。
如果您查看 TypeError,output
是类型 DetailedResponse
的一个实例。因此,您需要将代码修改为使用适当的对象封装
print(json.dumps(output.get_result(), indent = 4))
或者因为它不受保护 属性。
print(json.dumps(output.result, indent = 4))
幸运的是 output.result
是 json 可序列化的。
我使用 IBM 语音转文本服务从音频文件中获取文本。
这就是数据的样子
{
"result": {
"results": [
{
"alternatives": [
{
"confidence": 0.6,
"transcript": "state radio "
}
],
"final": true
},
{
"alternatives": [
{
"confidence": 0.77,
"transcript": "tomorrow I'm headed to mine nine
consecutive big con I'm finna old tomorrow I've got may meet and greet
with whoever's dumb enough to line up for that and then on Friday you can
catch me on a twitch panel"
}
],
"final": true
我试着用
把它变成 JSON print(json.dumps(output, indent = 4))
但是报错
TypeError: Object of type DetailedResponse is not JSON serializable
如何使用此数据仅打印 "transcript"?
json.dumps()
将 Python 对象转换为 JSON 字符串,这是由 API 样本完成的,用于记录/打印响应,但有一个怪癖Python 3.7 是什么使 Python 对象 json 可序列化发生了变化。
如果您查看 TypeError,output
是类型 DetailedResponse
的一个实例。因此,您需要将代码修改为使用适当的对象封装
print(json.dumps(output.get_result(), indent = 4))
或者因为它不受保护 属性。
print(json.dumps(output.result, indent = 4))
幸运的是 output.result
是 json 可序列化的。