python - Google Speech to Text - 无法将输出写入文本文件
问题描述
我正在尝试写入文本文件。它似乎每次都失败。我可以写 .write("test") 但将谷歌转录输出写入文件似乎失败了。
任何建议将不胜感激。
import speech_recognition as sr
from os import path
from pprint import pprint
audio_file = path.join(path.dirname(path.realpath(__file__)), "RobertP.wav")
r = sr.Recognizer()
with sr.AudioFile(audio_file) as source:
audio = r.record(source)
try:
txt = r.recognize_google(audio, show_all=True)
pprint (txt)
except:
print("Didn't work.")
try:
f = open("tester.txt", "w+")
f.write(txt)
f.close()
except:
print("Couldn't write to file")```
解决方案
看起来 txt 不是字符串。您可以尝试使用 str(txt) 将其转换为字符串。
顺便说一句:最好删除 try/except 以获取错误的测试原因。程序运行后,您可以再次添加它。
推荐阅读
- ios - 模式演示风格 iOS 13 上的横向应用程序
- node.js - 在侦听 MQTT 消息的 pm2-daemonized nodejs 脚本中,如何还侦听特定的击键?
- protractor - Cucumber @rerun 生成多个文件
- ios - 如何在 Swift 中的消息传递应用程序的后台获取新消息
- sql-server - 外部数据源和外部表的 Azure SQL 数据库构建失败
- c - 创建自己的 Web 服务器时无法连接到 localhost
- database - 持久化地图的数据库建议
> 堆外 - python - 在熊猫中获得每周百分位数?
- scala - 为什么 scala 在包装 EitherT 时不能推断出正确的类型?
- bash - 如何运行为不同的基础库版本构建的程序?