【问题标题】:How to avoid extra single quotation marks如何避免多余的单引号
【发布时间】:2016-02-03 04:22:28
【问题描述】:

如何避免多余的单引号

keywords = "van","buren","william","henry","harrison"

当我打印关键字时

打印关键字

输出:

“范”、“布伦”、“威廉”、“亨利”、“哈里森”

很好的控制台窗口

当我在查询中使用相同的变量时

          "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"]***
}}]}}}

【问题讨论】:

  • 你就不能{'content':keywords[:]}吗?
  • 感谢@Jason,非常好的技术

标签: python string


【解决方案1】:

keywords 打印为一个大字符串(用单引号括起来),因为这是您告诉它的:

{
    "terms": {
        "content": **["%s" % (keywords)]**
    } 
}

如果您希望将关键字打印为单个字符串的列表,只需这样打印:

{
    "terms": {
        "content": keywords
    } 
}

【讨论】:

  • 感谢@Gordon,我想在方括号 ["van","buren" 中打印像 "van","buren","william","henry","harrison" 这样的字符串“威廉”、“亨利”、“哈里森”]。当我按照您的方法打印字符串时,已生成以下结果。 { "条款": { "内容": 关键字 } } 结果 ===== { "条款": { "内容": \\"van\\",\\"buren\\",\\"william\ \",\\"亨利\\",\\"哈里森\\" } }
  • @SaqibUllah 你想让[ ] 成为content 中字符串的一部分吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-07
  • 2012-04-24
  • 1970-01-01
  • 2019-01-02
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
相关资源
最近更新 更多