简体   繁体   English

替换Python中字符串的多个字符

[英]Replace multiple characters of a string in Python

I have a column called Review Text with details like:我有一个名为Review Text的专栏,其中包含以下详细信息:

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.我编写了以下代码,用空白空间替换所有缩略词和反斜杠 ( \ ),并使用 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.但是当我再次查看我的字符串时,我也看到了一些正斜杠,现在我无法弄清楚如何在替换方法中添加它,因为它无法采用多个 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("/","")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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