如何避免额外的单引号
keywords =&#34; van&#34;,&#34; buren&#34;,&#34; william&#34;,&#34; henry&#34;,&#34; harrison&#34; < / p>
当我打印关键字时
打印关键字
输出:
&#34;货车&#34;&#34;布伦&#34;&#34;威廉&#34;&#34;亨利&#34;&#34;哈里森&#34;
在控制台窗口上很好
当我在查询中使用相同的变量时
"fields": ["name","contextType"],
"query": {
"bool": {
"must": [
{
"match": { "contextType": "{}".format(querystring)}
},
{
"terms": {
"content": **["%s" % (keywords)]**
}
}
]
}
}
单引号自动添加
{'query': {'bool': {'must': [{'match': {'contextGraph.contextType': 'president'}}, {'terms': {'content': ***['"van","buren","william","henry","harrison"']***
}}]}}}
{'query': {'bool': {'must': [{'match': {'contextGraph.contextType': 'president'}}, {'terms': {'content': ***["van","buren","william","henry","harrison"]***
}}]}}}
答案 0 :(得分:3)
关键字打印为一个大字符串(用单引号括起来),因为这就是你告诉它要做的事情:
{
"terms": {
"content": **["%s" % (keywords)]**
}
}
如果您希望将关键字打印为单个字符串列表,请按以下方式打印:
{
"terms": {
"content": keywords
}
}