# -*- coding: utf-8 -*-
__author__ = 'gyx'
__date__ = '2019/12/30 17:14'
import pandas as pd
import os
from collections import Counter
def add_keys(political_path,data_key,key):
if key not in data_key:
f = open(political_path, 'a+', encoding='utf-8')
f.write('\n'+key)
print('已插入关键词:',key)
else:
print('关键词字典中已存在该关键词')
if __name__ == '__main__':
path_file = os.path.dirname(os.path.abspath(__file__))
political_path = os.path.join(path_file, "political_keys.txt")
data = pd.read_csv(political_path, encoding='utf-8')
data_key = dict(Counter([k[0] for k in data.values]))
add_keys(political_path,data_key,'中央政治局常委')
未添加关键词前:
添加关键词 中央政治局常委:
再次运行关键词 中央政治局常委: