Python 断句子

Python 断句子

import random
import string
import time
import os, sys
def content_list():
    with open('1.txt', 'r', encoding='utf-8') as f:
        for i in f:
            i1 = i.replace('。','。\n')
            #print(i1)
            if i1:
                with open('2.txt', 'a', encoding='utf-8') as f2:
                    f2.write(i1)

#
def split_conent():
#获得当前系统时间的字符串
    localtime=time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))
    print('localtime='+localtime)
#系统当前时间年份
    year=time.strftime('%Y',time.localtime(time.time()))
#月份
    month=time.strftime('%m',time.localtime(time.time()))
#日期
    day=time.strftime('%d',time.localtime(time.time()))
#具体时间 小时分钟毫秒
    mdhms=time.strftime('%m%d%H%M%S',time.localtime(time.time()))
    num = 1
    totall = 0
    with open('2.txt', 'r', encoding='utf-8') as f:
        for i2 in f.readlines():
            print(i2,len(i2))
            random_str = ''.join(random.sample(string.ascii_letters + string.digits, 18))
            with open(year+month+day+mdhms+'word_{}.txt'.format(num), 'a', encoding='utf-8') as f2:
                if len(i2) >= 25:
                    f2.write(i2)
                    if totall == 2000:
                        num += 1
                        totall = 0
                    else:
                        totall += 1
                    print(totall, num)
                else:
                    f2.write(i2.strip())

content_list()
split_conent()

# 列出目录
print ("目录为 :/home/txt/dz断章/%s" %os.listdir(os.getcwd()))

# 移除
os.remove("1.txt")
os.remove("2.txt")
# 移除后列出目录
print ("移除后 :/home/txt/dz断章/%s" %os.listdir(os.getcwd()))


# Script   Python  

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×