V2EX › Python
这一行代码没看懂:为什么把正常的 Print(transcript)放在 except 后面输出?
By lisisi at 2025 年 7 月 8 日 · 2601 次点击模型输出 audio 和 transcript:
audio_string = ""
for chunk in completion:
if chunk.choices:
if hasattr(chunk.choices[0].delta, "audio"):
try:
audio_string += chunk.choices[0].delta.audio["data"]
except Exception as e:
print(chunk.choices[0].delta.audio["transcript"])
else:
print(chunk.usage)
wav_bytes = base64.b64decode(audio_string)
audio_np = np.frombuffer(wav_bytes, dtype=np.int16)
sf.write("audio_assistant_py.wav", audio_np, samplerate=24000)
第 7 、8 行:为什么把 print(chunk.choices[0].delta.audio["transcript"]) 放在 Exception 后面输出?
3 条回复 • 2025-07-08 14:33:44 +08:00
|
1
lisisi OP ```
if "data" in chunk.choices[0].delta.audio: audio_string += chunk.choices[0].delta.audio["data"] elif "transcript" in chunk.choices[0].delta.audio: print(chunk.choices[0].delta.audio["transcript"]) ``` |
|
2
NoOneNoBody 2025 年 7 月 8 日
except 内的 print 只是取代原有的出错信息,让代码可以继续运行,意义不大,有些人甚至只给个 pass
如果这段代码是别人写的,这个 print 只是随便打个断点信息,你可以改成你能理解的,或者输出到 log 方便以后查找 |
|
3
guanzhangzhang 2025 年 7 月 8 日
可能是偷懒选择一个随便思考的解决方式
|
推荐学习书目
› Learn Python the Hard Way
Python Sites
› PyPI - Python Package Index
› http://diveintopython.org/toc/index.html
› Pocoo
值得关注的项目
› PyPy
› Celery
› Jinja2
› Read the Docs
› gevent
› pyenv
› virtualenv
› Stackless Python
› Beautiful Soup
› 结巴中文分词
› Green Unicorn
› Sentry
› Shovel
› Pyflakes
› pytest
Python 编程
› pep8 Checker
Styles
› PEP 8
› Google Python Style Guide
› Code Style from The Hitchhiker's Guide