0

如何用引号 ( "s) 将 JSON 响应中的特定文本括起来?我正在使用 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.

即使脚本正确解析文本,TTS 也只会说出描述中的最后一个词。即使我使用两个字符串的组合和单独的字符串,就像我在上面的代码中所做的那样。

有什么问题?

4

1 回答 1

1

引号换行不是一个特定的问题tts,它只是当参数中有空格时参数解析/定界。

当您没有将文本括在引号中时,可能会发生这种情况,即句子中的所有单词都作为参数传递给tts,并且程序只考虑最后一个。

要解决此问题,您不应该那样使用subprocess.call,而是使用参数列表语法,它在需要时使用引号/引号引号字符保护参数:

subprocess.call(['tts.exe','-f','10','-v','4',phrase])
于 2017-03-07T22:39:38.053 回答