2017-03-07 43 views
0

如何用引号(" s)附带JSON响应中的特定文本?我正在使用Python 3.6.0。如何用引号将json响应中的特定文本括起来?我在Windows 8中使用Python 3.6.0

我的脚本使用了Cloudsight图像识别API。这允许我上传图像并从Cloudsight获取该图像的描述。

现在,我想要做的是使用TTS命令行工具来说出来自Cloudsight的响应。我使用的TTS是https://github.com/brookhong/tts

我的问题是,如果TTS只能用引号(" s)括起来,那么它只能说出字符串。否则,它只会说出字符串中的最后一个单词。 这里是我试过到目前为止:

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. 

即使脚本正确地分析文本,语音合成只讲了描述中的最后一个字。即使我使用两个字符串的组合,以及单独的字符串,就像我在上面的代码中所做的那样。

什么可能是错的?

回答

1

引用包装不是tts的特定问题,它只是在参数中有空格时参数解析/分隔。

如果不是将文本括在引号中,可能会发生这种情况:句子中的所有单词都会作为参数传递给tts,程序只会考虑最后一个。

为了解决这个问题,你不应该使用subprocess.call这样,但使用的参数列表语法代替它保护论点需要时引号/引号引用字符:

subprocess.call(['tts.exe','-f','10','-v','4',phrase]) 
+0

这是真棒! - 它马上解决了我的问题! :) – MichaelFuentes

+0

这里是我的代码的工作版本: image_results = get_results_for_token(image_token,API_KEY) 短语= ' “我认为,这显示”' 描述=(image_results [ '名']) 组合=短语+描述 导入子程序 subprocess.call(['tts.exe',' - f','10',' - v','4',combination]) print(combination) 再次感谢Jean-François! :) – MichaelFuentes

相关问题