[英]Google Speech to Text - Can't write output to text file
I am attempting to write to a text file.我正在尝试写入文本文件。 It just seems to fail every time.它似乎每次都失败了。 I can write.write("test") but writing the google transcription output to file seems to fail.我可以 write.write("test") 但将 google 转录输出写入文件似乎失败了。
Any advice would be greatly appreciated.任何建议将不胜感激。
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")```
It Looks like txt is not a string.看起来 txt 不是字符串。 You can try to convert it to a string with str(txt).您可以尝试使用 str(txt) 将其转换为字符串。
btw: It's better to remove the try/except for testing reasons to get the error.顺便说一句:最好删除 try/except 用于测试原因以获得错误。 After the program works you can add it again,.程序运行后,您可以再次添加它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.