【python技巧實用篇】python讀寫檔案、jieba自定義字典
阿新 • • 發佈:2018-12-26
import jieba
from astropy.table.np_utils import join
import os
import sys
import jieba.posseg as pseg
def main():
current_dir = os.path.abspath('.')
#自定義詞典檔案last檔案
dict_file = os.path.join(current_dir, 'last.txt')
jieba.load_userdict(dict_file)
#待分詞的檔案
file_name = os.path.join(current_dir, 'cutTest.txt')
f = open(file_name, encoding="UTF8")
line = f.readline()
#分詞之後寫入result檔案
file_name2 = os.path.join(current_dir, 'result.txt')
f2 = open(file_name2, 'w', encoding='utf8')
while line:
seg_list = jieba.cut(line, cut_all=False)
seg_list = " ".join(seg_list)
seg_list.encode("utf8" )
f2.write(seg_list)
f2.write("\n")
line = f.readline()
f2.close()
f.close()
print("end")
if __name__ == '__main__':
main()