Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python3中获取一个列表,并将其转换为字符串,但转义双引号,以便作为azure搜索的搜索参数传递_Python_Python 3.x_Azure Cognitive Search - Fatal编程技术网

在python3中获取一个列表,并将其转换为字符串,但转义双引号,以便作为azure搜索的搜索参数传递

在python3中获取一个列表,并将其转换为字符串,但转义双引号,以便作为azure搜索的搜索参数传递,python,python-3.x,azure-cognitive-search,Python,Python 3.x,Azure Cognitive Search,我有一个python应用程序,它接受一个文本(字符串)列表,我们希望在Azure认知搜索中将其用作搜索词。搜索参数必须是字符串,因此如果我有一个单词列表,我可以执行以下操作: words_to_search_list = ["toy", "durable"] words_to_search_str = ' '.join(words_to_search_list) 然后在Azure search中将words_传递给_search_str作为“search”参数,它可以搜索包含“持久”或“玩具

我有一个python应用程序,它接受一个文本(字符串)列表,我们希望在Azure认知搜索中将其用作搜索词。搜索参数必须是字符串,因此如果我有一个单词列表,我可以执行以下操作:

words_to_search_list = ["toy", "durable"]
words_to_search_str = ' '.join(words_to_search_list)

然后在Azure search中将words_传递给_search_str作为“search”参数,它可以搜索包含“持久”或“玩具”的文本

但是,我不知道如何处理单词“搜索”列表中有“大图”或“三角图”的情况,如下所示:

words_to_search_list = ["more toys", "free treats"]
为了从Azure获取包含“更多玩具”或“免费款待”的文本,我们需要如下传递参数:

"\"more toys\" \"free treats\""
这意味着bigram需要用双引号括起来,但要转义。 我开始这样做:

words_to_search_str=""
for words in words_to_search_list:
    words_list=words.split()
    if len(words_list)>1:
        words_escaped='\\"'+ words + '\\"' 
        words_to_search_str+=words_escaped
    else:
        words_to_search_str+=words
但这使得单词搜索到以下内容:

'\\"more toys\\"\\"free treats\\"'
这不是我想要的(双后挡板不起作用)

有没有任何方法可以获取该字符串列表并以一个字符串结束,但其中的bigram都在(转义的)双引号中


编辑:我想在我这里的解决方案中添加一点,如果你打印它,你会得到看起来是正确的对象(单反斜杠,而不是双反斜杠),但是实际的对象似乎仍然有两个反斜杠,当您传递到搜索参数时,它们不会给出相同的结果…

当您显示字符串时,基本上是以python语法输入字符串的方式。双反斜杠并不是真正的双反斜杠,就像在编写python代码时使用双反斜杠通过转义来指示实际的反斜杠一样,python只是在这么做。这也是双引号没有被转义的原因,它用单引号显示字符串。我希望这是有帮助的

以下内容将为您提供该格式:

words_to_search_list = ["more toys", "free treats"]
updated_words = ['\\"{}\\"'.format(words) for words in words_to_search_list]
words_to_search_str = '"{}"'.format(' '.join(updated_words))
print(words_to_search_str)

如果您运行的是3.6+,则应该这样做:

words_to_search_list = [
    "toy", "durable", "more toys", "free treats", "big durable toys"
]

words_to_search_str = '\"search\": \"' + ' '.join([
    f'\\"{word}\\"' if ' ' in word else word for word in words_to_search_list
]) + '\"'

print(words_to_search_str)
如果没有,请尝试:

words_to_search_list = [
    "toy", "durable", "more toys", "free treats", "big durable toys"
]

words_to_search_str = '\"search\": \"' + ' '.join([
    '\\"{}\\"'.format(word) if ' ' in word else word for word in words_to_search_list
]) + '\"'

print(words_to_search_str)

这个问题源于您转义\:
words \'u转义='\\'''+words+'\'''\''''

你应该逃避,但“像:
words\u转义=“\”+words+“\”


这应该会产生预期的结果

奇怪的是,我在Python3.6、3.7和3.8中得到了相同的输出:“搜索”:“玩具耐用\”更多玩具\“免费款待\“大耐用玩具\”“。您能复制并粘贴准确的错误消息吗?啊,如果您正在运行第二个解决方案,则不再显示错误,但打印时的单词\u to\u search\u str与实际单词之间似乎存在差异。”。当你打印它时,它看起来是正确的,但如果你只是看它是什么,它似乎仍然有双反斜杠。。。我更新了问题,使之更清楚。另外,“search”不应该是字符串的一部分(也更新了它以使问题更清楚),打印时的单词与实际单词之间似乎存在差异。当你打印它时,它看起来是正确的,但如果你只是看它是什么,它似乎仍然有双反斜杠。。。我更新了问题,使之更清楚。
words_to_search_list = [
    "toy", "durable", "more toys", "free treats", "big durable toys"
]

words_to_search_str = '\"search\": \"' + ' '.join([
    '\\"{}\\"'.format(word) if ' ' in word else word for word in words_to_search_list
]) + '\"'

print(words_to_search_str)