代码拉取完成,页面将自动刷新
# -*- coding: utf-8 -*-
"""
数据格式:
#正文 0
#1级 【政治学】 1
#2级 == 历史 == 2
#3级 === 古典时期 === 3
#4级 ==== 古典时期 ==== 4
# 平行关系:* 5
清洗逻辑:
目前是按行保存在txt之中,给每一行打上标签。
"""
from tqdm import tqdm
import pandas as pd
def adj_1(sting,limit = '【'):
try:
if sting.index(limit) < 1:
result = True
else:
result = False
except:
result = False
return result
#清洗函数
def wiki_clean(txt_path):
# 加载并读出
f = open(txt_path,encoding = 'utf-8')
f_txt = []
for line in f.readlines():
f_txt.append(line)
# 标注
judge_list = []
for txt in tqdm(f_txt):
if '【' in txt and '】' in txt and adj_1(txt):
judge_list.append(1)
elif '====' in txt and adj_1(txt,limit = '===='):
judge_list.append(4)
elif '===' in txt and adj_1(txt,limit = '==='):
judge_list.append(3)
elif '==' in txt and adj_1(txt,limit = '=='):
judge_list.append(2)
elif '*' in txt and adj_1(txt,limit = '*'):
judge_list.append(5)
else:
judge_list.append(0)
# dataframe
wiki_dataframe = pd.DataFrame(list(zip(f_txt,judge_list)))
return wiki_dataframe
if __name__ == '__main__':
txt_path = 'E:\matt\get\wiki\zhwiki.txt'
wiki_dataframe = wiki_clean(txt_path)
wiki_dataframe.to_csv('./wiki.csv',index = False,encoding = 'utf-8')
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。