Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python .replace() function, removing backslash in certain way

Tags:

python

I have a huge string which contains emotions like "\u201d", AS WELL AS "\advance\"

all that I need is to remove back slashed so that:

- \u201d = \u201d
- \united\ = united

(as it breaks the process of uploading it to BigQuery database)

I know it should be somehow this way:

string.replace('\','') But not sure how to keep \u201d emotions.

ADDITIONAL: Example of Unicode emotions

  • \ud83d\udc9e
  • \u201c
  • \u2744\ufe0f\u2744\ufe0f\u2744\ufe0f
like image 520
Bobbby Avatar asked Jun 09 '26 04:06

Bobbby


2 Answers

You can split on all '\' and then use a regex to replace your emotions with adding leading '\'

s = '\\advance\\\\united\\ud83d\\udc9e\\u201c\\u2744\\ufe0f\\u2744\\ufe0f\\u2744\\ufe0f'
import re
print(re.sub('(u[a-f0-9]{4})',lambda m: '\\'+m.group(0),''.join(s.split('\\'))))

As your emotions are 'u' and 4 hexa numbers, 'u[a-f0-9]{4}' will match them all, and you just have to add leading backslashes

First of all, you delete every '\' in the string with either ''.join(s.split('\\')) or s.replace('\\')

And then we match every "emotion" with the regex u[a-f0-9]{4} (Which is u with 4 hex letters behind)

And with the regex sub, you replace every match with a leading \\

like image 117
BlueSheepToken Avatar answered Jun 10 '26 19:06

BlueSheepToken


You could simply add the backslash in front of your string after replacement if your string starts with \u and have at least one digit.

import re

def clean(s):

    re1='(\\\\)' # Any Single Character "\"
    re2='(u)'    # Any Single Character "u"
    re3='.*?'    # Non-greedy match on filler
    re4='(\\d)'  # Any Single Digit

    rg = re.compile(re1+re2+re3+re4,re.IGNORECASE|re.DOTALL)
    m = rg.search(s)

    if m:
        r = '\\'+s.replace('\\','')
    else:
        r = s.replace('\\','')
    return r


a = '\\u123'
b = '\\united\\'
c = '\\ud83d'

>>> print(a, b, c)
\u123 \united\ \ud83d

>>> print(clean(a), clean(b), clean(c))
\u123 united \ud83d

Of course, you have to split your sting if multiple entries are in the same line:

string = '\\u123 \\united\\ \\ud83d'
clean_string = ' '.join([clean(word) for word in string.split()])
like image 43
alec_djinn Avatar answered Jun 10 '26 19:06

alec_djinn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!