from string import ascii_letters
with open( 'scripture.txt' ) as fin :
text = fin.read()
valid_characters = ascii_letters + '\n\t '
text = ''.join( t for t in text if t in valid_characters )
text = text.split()
for i in range(len(text)//1000) :
with open( 'part_%03d.txt' % i, 'w') as fout :
thousand_words = text[i*1000:min((i+1)*1000,len(text))]
fout.write( ' '.join( thousand_words ))
with open('scripture_verses.txt') as f:
words = []
i = 0
for line in f:
for word in line.split():
words.append(word)
i += 1
if i % 1000 == 0:
with open('out{}.txt'.format(i // 1000), 'w') as out:
print(' '.join(words), file=out)
words = []
else:
with open('out{}.txt'.format(i // 1000 + 1), 'w') as out:
print(' '.join(words), file=out)
words = []
这应该起作用:
相关问题 更多 >
编程相关推荐