如何用引号("
s)附上JSON响应中的特定文本?我使用的是Python 3.6.0。
我的脚本使用Cloudsight图像识别API。这允许我上传图像并从Cloudsight获取该图像的描述。
现在,我尝试做的是使用TTS命令行工具来说明来自Cloudsight的响应。我使用的TTS是https://github.com/brookhong/tts
我的问题是,如果用引号("
s)括起来,这个TTS只会说出字符串。否则,它只会说出字符串中的最后一个单词。
这是我到目前为止所尝试的:
image_results = get_results_for_token(image_token, api_key) # This gets the JSON response from Cloudsight.
phrase = '"I think this shows "'
description =(image_results['name']) # This is for getting the string that I want from the JSON response.
combination = phrase + description
import subprocess
subprocess.call('tts.exe -f 10 -v 4 '+combination, shell=False) # Initialize TTS to speak out combination of phrase and description, which does not work as TTS only speaks out last word in description.
subprocess.call('tts.exe -f 10 -v 4 '+phrase, shell=False) # Try to speak out the phrase first, which works because it's enclosed by quotes.
subprocess.call('tts.exe -f 10 -v 4 '+description, shell=False) # Try to speak out description, which does not work. TTS only speaks out last word probably because the name string from the JSON response is not enclosed by quotes.
print(combination) # This works. The script is parsing the text correctly.
即使脚本正确解析文本,TTS也只能说出描述中的最后一个单词。即使我使用两个字符串和单独的字符串的组合,就像我在上面的代码中所做的那样。
可能出现什么问题?
答案 0 :(得分:1)
引用包装不是tts
的特定问题,它只是在参数中有空格时进行参数解析/分隔。
当您未将文字括在引号中时,可能会发生这样的情况:句子中的所有单词都作为参数传递给tts
,程序只考虑最后一个。
要解决这个问题,你不应该像这样使用subprocess.call
,而是使用参数列表语法代替,在需要时用引号/引号引用字符来保护参数:
subprocess.call(['tts.exe','-f','10','-v','4',phrase])