import pandas as pd
# 加载数据集
df = pd.read_json('data/people.json')
# 定义违禁词列表 自己定义
banned_words = ['16']
# 过滤数据集
for index, row in df.iterrows():
question = row['name'] #数据集里面的input output
answer = row['age']
# 检查问题和答案是否包含违禁词
if any(word in question for word in banned_words) or any(word in answer for word in banned_words):
# 删除包含违禁词的数据
df.drop(index, inplace=True)
#实现去除乱码和标点符号
def content_filter(text):
# 去除乱码
text = re.sub(r'[^\x00-\x7F]+', '', text)
# 去除多余的标点符号
text = text.translate(str.maketrans('', '', string.punctuation))
return text
# 打印过滤后的数据集
df['name'] = df['name'].apply(content_filter)
df['age'] = df['age'].apply(content_filter)
#将数据保存到json文件里
df.to_json('afterfixeson',orient='records')
print(df)
将原始json文件中按照规则进行去除
去除之后的数据为:
完成规则过滤