-2
  • My UI is sending %20 for space

  • I need to replace %20 with space

  • The incoming data may be string or list

Below are the condition need to satisfied

My input are input Lets say my search bar is searching below pattern

ABC,%20CDE Expected out ABC,CDE

ABC%20CDE Expected out ABC CDE

ABC%20%20%20CDE Expected out ABC CDE

[ABC,%20CDE] Expected out [ABC,CDE]

ABC,%20%20%20 Expected out ABC

[ABC,%20%20%20] Expected out [ABC]

[,%20%20%20CDE] Expected out [CDE]

,%20%20%20CDE Expected out CDE

My Code only satisfied one condition

string.replace('%20',' ')

I am searching with replace. If not regex also would help

1 Answers1

0

Use unquote from urlib.parse

from urllib.parse import unquote


unquote("ABC,%20CDE")       # 'ABC, CDE'
unquote("[ABC,%20CDE]")     # '[ABC, CDE]'
unquote("ABC%20CDE")        # 'ABC CDE'
unquote("ABC%20%20%20CDE")  # 'ABC   CDE'
unquote(",%20%20%20CDE")    # ',   CDE'

Then you can use replace to replace extra spaces.

Docs: https://docs.python.org/3/library/urllib.parse.html

forzagreen
  • 2,509
  • 30
  • 38