python-小知识点 ---- 多字符串替换

一个长字符串或者一个文本文件做数据分析的时候经常遇到需要排除干扰项的需求,这时候就需要多字符串替换的功能

使用str的 replace函数

字符串常用的替换函数

比如说将标点替换成,使用replace连续替换多次即可

s = '''
	There was a card party at the rooms of Naroumoff, of the Horse Guards. 
	The long winter night passed away imperceptibly, and it was five o'clock in the morning before the company sat down
to supper. Those who had won ate with a good appetite; the others sat staring absently at their empty plates. When the 
champagne appeared, however, the conversation became more animated, and all took a part in it.
'''

b = s.replace(",", "").replace(".", "").replace(";", "")
print(b)

使用re 的sub函数替换

可以使用正则表达式来筛选你要替换的内容
如果你有指定的需要替换的字符串列表,且都是替换成同一个对象可以这样:

import re

s = '''
	There was a card party at the rooms of Naroumoff, of the Horse Guards. 
	The long winter night passed away imperceptibly, and it was five o'clock in the morning before the company sat down
to supper. Those who had won ate with a good appetite; the others sat staring absently at their empty plates. When the 
champagne appeared, however, the conversation became more animated, and all took a part in it.
'''
words = ["When", "There", "Those", "The"]

def replace_re():
	pattern = re.compile("|".join(words))
	print(pattern)
	r = re.sub(pattern, "", s)	# 全部替换成空
	print(r)

如果你需要指定字符替换成指定字符的话:

import re

s = '''
	There was a card party at the rooms of Naroumoff, of the Horse Guards. 
	The long winter night passed away imperceptibly, and it was five o'clock in the morning before the company sat down
to supper. Those who had won ate with a good appetite; the others sat staring absently at their empty plates. When the 
champagne appeared, however, the conversation became more animated, and all took a part in it.
'''
words = {"When": "when", "There": "there", "Those": "those", "The": "the"}

def replace_re():
	rep = dict((re.escape(k), v) for k, v in words.items())
	pattern = re.compile("|".join(rep.keys()))
	r = pattern.sub(lambda m: rep[re.escape(m.group(0))], s)
	print(r)

你可能感兴趣的:(python-小知识)