Answer the question
In order to leave comments, you need to log in
Python how to split text file into multiple by line length?
There is a huge text file called words_alpha.txt , of the format:
Слово1
Слово2
Слово3
и т.д.
Answer the question
In order to leave comments, you need to log in
f_handlers = {}
with open('words_alpha.txt', 'r') as inp_file:
for line in inp_file:
w_len = len(line.strip())
if w_len == 0: # исключаем слова с нулевой длиной
continue
fn = f'words_{w_len}.txt'
f = f_handlers.setdefault(fn, open(fn, 'w+'))
f.write(line)
for handler in f_handlers.values():
handler.close()
Well, you read and write to different files according to the length of the line.
py "('x'*random.randint(1, 10) for _ in range(100))" | \
py -x "open(f'tmp/path_to_dest_folder/words_len={len(x):05}.txt', 'a').write(x+'\n')"
cat big_file.txt
. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question