1.str.replace()
str.replace(old, new[, max])
用新字符串 new 替换旧字符串 old,可以设置替换次数 max
old – 将被替换的子字符串。
new – 新字符串,用于替换old子字符串。
max – 可选字符串, 替换不超过 max 次
text1 = ' Info has been found (+/- 100 pages, and 4.5 MB of .pdf files) Now i have to wait untill our team leader has processed it and learns html. '
text1

# str.replace(old, new[, max])
text1.replace(' ', '')
需要注意的是,replace 不会改变原 string 的内容!
2.re.sub
re.sub(pattern, repl, string, count=0, flags=0)
使用正则表达式来进行子串的替换
pattern:表示正则表达式中的模式字符串(被替换的部分);
repl:替换后的部分(既可以是字符串,也可以是函数);
string:要被处理的,要被替换的字符串;
count:匹配的次数, 默认是全部替换

# re.sub(pattern, repl, string, count=0, flags=0)
re.sub('\W', ' ', text1) # 将任何不是单词的字符替换为空格
369



被折叠的 条评论
为什么被折叠?



