take a list in python3 and make into a string but escape double quotes in order to pass as search parameter for azure search

Related searches

I have an application in python that accepts a list of text (strings) that we want to use as search terms in Azure Cognitive Search. The search parameter needs to be a string, so if I have a list of words I can do something like:

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

and then pass words_to_search_str as the "search" parameter in Azure Search, and it can search for text that has "durable" or "toy".

"toy durable"

However, I am not sure how to handle situations where there are bigrams or trigrams in the words_to_search_list like here:

words_to_search_list = ["more toys", "free treats"]

In order to get back text from Azure that contains either "more toys" or "free treats" we'd need to pass the parameter like this:

"\"more toys\" \"free treats\""

Meaning the bigrams need to be in double quotes, but escaped. I started this:

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

But this makes words_to_search_str into the following:

'\\"more toys\\"\\"free treats\\"'

which is not what I want (the double backsplashes won't work).

Is there any way to take that list of strings and end up with one string, but where the bigrams are each in (escaped) double quotes?

Edit: I'd like to add that in the solution I have here, if you print it, you get what looks to be the right object (single backslashes, not double), but the actual object still seems to have the double backslashes and they don't give the same result when you pass into the search parameter...

This should do it if you are running 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)

If not, try:

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)

Backslash is added to json before double quote, 5 Jul 2018 I am looking for way to parse the below result as json object and get each how to convert string with escape characters to json You'd have to convert� create_list is building a list of all the items in the string form of data.And so you can change it to: def create_list(data): return list(str(data)) I find it easier to read create_command if you merge some of the lines together:

When you display a string it's basically how would enter the string as python syntax. The double backslash isn't really a double backslash, like when you wrote your python code you used the double back slash to indicate an actual backslash by escaping it, python is simply doing that. That's also the reason why the double quotes are not being escaped, it's showing the string in single quotes. I hope that was helpful

Html escape quotes, Escape Single Quotes within Azure Resource Template by Maik van der Gaag JavaScript String Escape / Unescape Escapes or unescapes a JavaScript string in an html environment. since double quotes is used in the parameter list for How do I escape an ampersand in a query executed in the SQL Worksheet in� In the second string, we use double quotes to enclose the string. So to put double quotes within the string, we must use a double quote escape character (\"). We use two of these in the above string. In the third string, we want to insert a tab into the string. In Python, a tab inserts 8 spaces into the string.

The following should give you that format:

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)

Python query string, Not only do we grab parameter values but we also modify them dynamically with Just call the method on the string object to search for a string, like so: obj. in May 20, 2020 � Overview A string is a list of characters in order. . query_string. py In this Python tutorial, we are going to show you how to escape quotes from a� Given a list, write a Python program to convert the given list to string. There are various situation we might encounter when a list is given and we convert it to string. For example, conversion to string from the list of string or the list of integer.

The problem arises from you escaping the \ : words_escaped='\\"'+ words + '\\"'

You should escape but the " like: words_escaped='\"'+ words + '\"'

That should produce the anticipated result

MySQL 8.0 Reference Manual :: 9.1.1 String Literals, Quoted strings placed next to each other are concatenated to a single string. a string quoted within double quotation marks is interpreted as an identifier. a backspace, but \B is interpreted as B . Escape processing is done according to the The \% and \_ sequences are used to search for literal instances of % and _ in� Convert list to string by join all the strings items in the list to create a concatenated string, def convert_list_to_string(org_list, seperator=' '): """ Convert list to string, by joining all item in list with given separator.

Making Sense of the Metadata: Clustering 4,000 Stack Overflow tags , Let's find tags that usually go together: Co-occurring tags on Stack Overflow BigQuery ML does a good job of hot-encoding strings, but it doesn't handle sprite, rdlc, game-engine, azure-search, azure-eventhub, physics, grouping, logic, key, filtering, parameter-passing, list-comprehension, apply,� 2. Using Double Quotes. You can also declare a string using double quotes in Python. The following is an example: a_string = "Hello World!" print(a_string) Output Hello World! 3. Handle Single Quote in a String. Suppose you have an apostrophe (‘) in the string, then you would not be able to declare a string variable with single quotes. Apply

Updated String :- Hello Python Escape Characters. Following table is a list of escape or non-printable characters that can be represented with backslash notation. An escape character gets interpreted; in a single quoted as well as double quoted strings.

Using a raw string with different string literals : We know that in python, we have multiple ways to write a string literal. We can use either a single quote, double quotes, double-triple quotes or single-triple quotes for a string literal. We can convert any of these string types to raw as well.

Comments
  • Odd, I get the same output in Python 3.6, 3.7, and 3.8: "search": "toy durable \"more toys\" \"free treats\" \"big durable toys\"". Can you copy and paste the exact error message?
  • Ah, if you are running <3.6, f-strings might be the cause of that error. Please see the updated answer.
  • The second solution no longer gives an error but there seems to be a difference between what words_to_search_str looks like when you print it vs what it is. When you print it it looks correct but if you just look at what it is it still seems to have the double backslashes... I updated the question to make that more clear. Also "search" shouldn't be part of the string (also updated that to make it more clear in the question)
  • There seems to be a difference between what words_to_search_str looks like when you print it vs what it is. When you print it it looks correct but if you just look at what it is it still seems to have the double backslashes... I updated the question to make that more clear.