我正在开发TouchDesigner的一个补丁,我希望它使用SpeechRecognition,并从表格中记录的单词/短语构建一个安装。要做到这一点,我希望能够将它打印的内容保存到文本文件中,可惜我是一个糟糕的程序员,到目前为止还不能让脚本完全工作。
需要澄清的是,语音识别部分正在工作,这是一个精简的原始脚本,只剩下Google API。我所需要的只是将结果(当它们被发送到控制台时)写入一个文本文件以供以后使用。
这是发送识别出的单词/短语的部分。
print(u"{}".format(value).encode("utf-8"))
else:
print("{}".format(value))我需要每次附加的词(而脚本连续运行)。
非常感谢您的帮助。
import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone()
try:
with m as source: r.adjust_for_ambient_noise(source)
while True:
with m as source: audio = r.listen(source)
print("")
try:
value = r.recognize_google(audio)
if str is bytes:
print(u"{}".format(value).encode("utf-8"))
else:
print("{}".format(value))
except sr.UnknownValueError:
print("")
except sr.RequestError as e:
print("{0}".format(e))
except KeyboardInterrupt:
pass发布于 2016-03-30 00:27:11
要将其输出到一个文件中,您可以执行如下操作
import speech_recognition as sr
r = sr.Recognizer()
m = sr.Microphone()
try:
with m as source: r.adjust_for_ambient_noise(source)
while True:
with m as source: audio = r.listen(source)
print("")
try:
value = r.recognize_google(audio)
if str is bytes:
result = u"{}".format(value).encode("utf-8")
else:
result = "{}".format(value)
with open("outputs.txt","a") as f:
f.write(result)
print(result)
except sr.UnknownValueError:
print("")
except sr.RequestError as e:
print("{0}".format(e))
except KeyboardInterrupt:
pass我的a with open ...所做的是附加到一个文件中。with x as y:是一种创建x作为y的蟒蛇方法,您只需要在这段脚本中使用它。open("all_outputs.txt","a")打开文件all_outputs.txt作为输出文件(如果它不存在就创建它),"a“将它设置为附加文件,所以它只是添加您在末尾编写的任何内容。f.write(result)会将结果写入该输出文件。
https://stackoverflow.com/questions/36283124
复制相似问题