skip to Main Content
{'[\n M a k i n g   c o f f e e   i s   n o t   r e l a t e d   t o   t h e   c o n t e x t   i n f o r m a t i o n   p r o v i d e d .]'}

I need this response as below:

Making coffee is not related to the context information provided.

I have tried as below:

import re
import json
import string
import re

my_int ={'[\n M a k i n g   c o f f e e   i s   n o t   r e l a t e d   t o   t h e   c o n t e x t   i n f o r m a t i o n   p r o v i d e d .]'}
a=[]
print(type(my_int))
result = re.sub(r'[0-9]', '_', str(my_int))

print(type(result))
print(" ".join(result.split()))
print(result.translate({ord(c): None for c in string.whitespace}))

print(re.sub(r"\s+", "", result), sep='')
print(re.sub(r"^\s+", "", result), sep='')
print(re.sub(r"\s+$", "", result), sep='')
print(re.sub(r"^\s+|\s+$", "", result), sep='')`


Tried all these ways to get the result but there is no success.

NOTE: Here as you see all the letters are separate characters, hence i am not able to remove the spaces between them.

Help would be much appreciated.

2

Answers


  1. What a horrible input!

    Someone has created for you a set, for no reason.

    Inside it they have put a single element, and that is not a string but an ugly way of putting a string in side [ and ] characters.

    You can extract it, though. A fudge for the spaces is to temporarily convert the double spaces into something else, remove all the other spaces, and then bring back the double spaces. Just make sure that the sequence you use, such as !DOUBLESPACE! cannot occur in your string. You might want to use international characters that are not used in your region of the world.

    my_set ={'[\n M a k i n g   c o f f e e   i s   n o t   r e l a t e d   t o   t h e   c o n t e x t   i n f o r m a t i o n   p r o v i d e d .]'}
    
    my_string = list(my_set)[0].replace("[","").replace("]","").replace("\n","").replace("  ","!DOUBLESPACE!").replace(" ","").replace("!DOUBLESPACE!"," ")
    
    print(my_string)
    

    No imports are needed.

    Result:

    Making coffee is not related to the context information provided.
    

    Type of result

    You definitely do not want a set result. Someone has given you a set as an input.

    The result of the above code, my_string, is just a string. Make sure to use that, and not the set, for onward processing.

    Login or Signup to reply.
  2. Try:

    import re
    
    my_int = {'[\n M a k i n g   c o f f e e   i s   n o t   r e l a t e d   t o   t h e   c o n t e x t   i n f o r m a t i o n   p r o v i d e d .]'}
    
    for s in my_int:
        s = re.sub(r'\.', '', s)
        s = re.sub(r's(?=S)|(?<=S)s', '', s)
        print(s)
    

    Prints:

    Making coffee is not related to the context information provided.
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search