python中如何替换字符串中的换行回车等特殊符号?


如题,想替换掉 '\n', '\t', ' ' 等符号,请问该如何操作?

符号替换 python 开发 特殊字符

请叫我恐鹊 9 years, 8 months ago

有几种方法,请参考:

  1. 直接调用函数 replace

    mystring.replace('\n', '').replace('\r', '').replace('\t', '').replace(' ', '')
    
  2. 使用 正则表达式:

    import re
    r_unwanted = re.compile("[\n\t\r]")
    r_unwanted.sub("", mystring)
    
张是张柏芝的芝 answered 9 years, 8 months ago

Your Answer