python 利用jieba库,解析路径下的简历文件(XML文件)
关键库
- os库:文件读取和输出
- re库:文本替换
- parse库:XML文件读取库
- csv库:csv文件处理库
- jieba库:NLP文本解析 分词库
思路
- 1 获取某一路径下所有简历数据(文件)
- 2 将所有简历文件进行依次遍历,解析XML数据内容
- 3 对复杂内容:履职经历,利用NLP 语言识别,进行分词。
- 4 对分词后结果按照需求情况进行处理和存储
- 5 将处理后的数据按照CSV文件进行输出和保存,便于后续分析
上代码
import os
import re
from xml.dom.minidom import parse
import csv
import jieba
filePath = r"E:\python-true\myprj\2021test"
allpath = []
filename = []
ids = []
data = []
allfilelist = os.listdir(filePath)
for file in allfilelist:
filepath = os.path.join(filePath, file)
if os.path.isfile(filepath):
if ((filepath.find(".lrmx") != -1) and (filepath.find("$") == -1)):
allpath.append(filepath)
ids.append(file[:6])
filename.append(file)
print("-----开始抽取数据-----")
for id_one in range(0, len(ids)):
document_tree = parse(allpath[id_one])
data_list = []
jianli_node = document_tree.getElementsByTagName("JianLi")[0]
jianli = jianli_node.childNodes[0].data
data_list.append(jianli)
jianli = re.sub("\n", "", jianli)
tempstr = jianli.split(";")
print("简历拆分后:")
print(tempstr)
date_begin = []
date_end = []
jobhistory = []
for strtemp in tempstr:
strtemp = re.sub(" ", "", strtemp)
seg_list = jieba.lcut(strtemp, cut_all=False, HMM=True)
date_begin.append(seg_list[0])
if (seg_list[2].find(".") != -1):
date_end.append(seg_list[2])
job_little = ""
for i_little in range(3, len(seg_list)):
job_little = job_little + seg_list[i_little]
jobhistory.append(job_little.strip())
else:
date_end.append(" ")
job_little = ""
for i_little in range(2, len(seg_list)):
job_little = job_little + seg_list[i_little]
jobhistory.append(job_little.strip())
xingming_node = document_tree.getElementsByTagName("XingMing")[0]
xingming = xingming_node.childNodes[0].data
for i in range(0, len(date_begin)):
data.append((ids[id_one], xingming, date_begin[i], date_end[i], jobhistory[i]))
csvfile = open('xml_change_ok.csv', 'w', encoding='utf-8')
writer = csv.writer(csvfile)
writer.writerow(["id", "XingMing", "date_begin", "date_end", "jobhistory"])
writer.writerows(data)
csvfile.close()
print("----输出完毕-----")
Enjoy it!