初始python 之 自动拆分转换文本内容
时间:2021-07-09 17:54:52
收藏:0
阅读:0
上一篇升级版,转换文件内容。
#!/user/bin env python # author:Simple-Sir # time:2021/7/9 23:32 def txt_2_list(filename): dic = {} dic_k = [] dic_v = [] with open(filename,‘r‘,encoding=‘utf-8‘) as f: for i in f.readlines(): j = i.strip(‘\n‘) # 删除换行符 li_k = j.split(‘,‘) # 以逗号分隔为列表 if len(li_k) == 2: dic[li_k[0]] = li_k[1] dic_k.append(li_k[0]) dic_v.append(li_k[1]) else: dic_k.append(li_k[0]) return dic,dic_k,dic_v def get_word_in_list(info,dic_k): word_li = [] while len(info) > 0: m = 0 n = 0 for i in range(len(info)+1): if info[:i] in dic_k: word_li.append(info[:i]) info = info[i:] n = 1 m = i if n == 0: word_li.append(info[0:1]) info = info[m+1:] return word_li def translat_word(word_li,dic,dic_k): get_v = [] for i in word_li: if i in dic_k: get_v.append(dic[i]) else: get_v.append(i) re_w = (‘_‘).join(get_v) return re_w if __name__ == ‘__main__‘: dict_file = ‘dir‘ info_file = ‘infofile‘ info_k=txt_2_list(info_file)[1] print(‘----------- 要转换的内容 ----------\n ‘,(‘\n ‘).join(info_k)) print(‘----------- 转换结果 ------------‘) for info in info_k: dic,dic_k,dic_v = txt_2_list(dict_file) word_li = get_word_in_list(info, dic_k) tw = translat_word(word_li,dic,dic_k) print(‘%s -->> %s‘%(info,tw))
字典库:
剪刀,jiandao
被子,beizi
笔,bi
我,wo
要,yao
要转换的文件内容:
我要剪刀
我不要剪刀
我要剪刀和笔
我要被子和剪刀
转换结果:
评论(0)