[英]How do I enclose with quotes a specific text from a json response? I'm using Python 3.6.0 in Windows 8
How do I enclose with quotes ( "
s) a specific text from a JSON response? I'm using Python 3.6.0. 如何用引号(附
"
S)从JSON响应特定的文本?我使用Python 3.6.0。
My script uses Cloudsight image recognition API. 我的脚本使用Cloudsight图像识别API。 This allows me to upload an image and get a description of that image from Cloudsight.
这使我可以上传图像并从Cloudsight获得该图像的描述。
Now, what I'm trying to do is use a TTS command line tool to speak out the response from Cloudsight. 现在,我正在尝试使用TTS命令行工具说出Cloudsight的响应。 The TTS that I use is https://github.com/brookhong/tts
我使用的TTS是https://github.com/brookhong/tts
My problem is that this TTS can only speak out strings if it's enclosed by quotes ( "
s). Otherwise, it will only speak out the last word in the string. Here is what I tried so far: 我的问题是,如果该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.
Even if the script is parsing the text correctly, the TTS only speaks out the last word in the description. 即使脚本正确地解析了文本,TTS也只会说出描述中的最后一个单词。 This is even if I use a combination of the two strings, and the separate strings, as I did in my code above.
即使我像上面的代码那样使用了两个字符串和单独的字符串的组合,也是如此。
What could be wrong? 有什么事吗
Quote-wrapping is not a specific problem to tts
, it's just about argument parsing/delimiting when there are spaces in the argument. 引号包装不是
tts
的特定问题,它仅是在参数中有空格时对参数进行解析/定界。
Which probably happens when you're not enclosing your text into quotes is that all words from the sentence are passed as arguments to tts
, and the program just considers the last one. 当您不将文本用引号引起来时,可能会发生以下情况:将句子中的所有单词作为参数传递给
tts
,并且程序仅考虑最后一个。
To fix this, you shouldn't use subprocess.call
like that, but use the argument list syntax instead which protects arguments with quotes/quotes quote chars when needed: 为了解决这个问题,您不应该那样使用
subprocess.call
,而应使用参数列表语法代替,该语法在需要时用引号/引号引起来保护参数:
subprocess.call(['tts.exe','-f','10','-v','4',phrase])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.