简体   繁体   中英

Replace multiple characters of a string in Python

I have a column called Review Text with details like:

Review text
i\'m glad i did bc i never would have ordered it/'s

I wrote the following code to replace all the contractions and backslashes ( \ ) with empty space and applied it using a lambda function. But when I again look at my string, I see some forward slash as well and now I can't figure out how to add it inside the replace method because it is not able to take multiple arguments.

def expanded(x):
    if type(x) is str:
        x = x.replace('\\', '')
        for key in contractions:
            value = contractions[key]
            x = x.replace(key, value)
        return x
    else:
        return x

Use

x = x.replace('\\', '').replace("/","")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM