简体   繁体   English

如何用引号将JSON响应中的特定文本括起来? 我在Windows 8中使用Python 3.6.0

[英]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.

相关问题 在 Python 如何从 Json 响应中获取特定字段? - In Python How do I get specific Fields from Json response? 如何从 json 响应中获取 output 特定数据? - How do I output specific data from a json response? 如何从此JSON响应中获取特定部分? - How do I get a specific part from this JSON response? 我找不到任何安装请求或lxml的方法,我使用的是python 3.6.0,并且我有一台装有64位OP系统的Windows计算机 - I can't find any way to install requests or lxml, i'm using python 3.6.0 and i have a windows computer with a 64 bit OP system 如何在 python 数组中用双引号替换单引号而不替换 json 值中的单引号? - How do I replace single quotes with double quotes in a python array without replacing the single quotes in the json values? 如何解析来自 Python 请求的 JSON 响应? - How do I parse a JSON response from Python Requests? 如何序列化来自 Python 中 API 的 JSON 响应? - How do I serialize this JSON response from API in Python? 如何从Python中的特定文本行中提取文本? - How do I pull text from a specific text line in Python? 如何使用 python 在字符串中搜索特定文本 - How do I search for specific text in a string using python 如何从双引号中提取文本并将其添加到字符串? python 3.x - How do i extract text from double quotes and add it to string ? python 3.x
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM