У меня есть один большой текстовый файл.Я разбил этот файл на небольшие файлы определенного размера.Ниже приведен пример, который я получаю:
import math
import os
numThread = 4
inputData= 'dir\example.txt'
def chunk_files():
nline = sum(1 for line in open(inputData,'r', encoding='utf-8', errors='ignore'))
chunk_size = math.floor(nline/int(numThread ))
n_thread = int(numThread )
j = 0
with open(inputData,'r', encoding='utf-8', errors='ignore') as file_:
for i, line in enumerate(file_):
if (i + 1 == j * chunk_size and j != n_thread) or i == nline:
out.close()
if i + 1 == 1 or (j != n_thread and i + 1 == j * chunk_size):
chunk_file = '_raw' + str(j) + '.txt'
if os.path.isfile(chunk_file):
break
out = open(chunk_file, 'w+', encoding='utf-8', errors='ignore')
j = j + 1
if out.closed != True:
out.write(line)
if i % 1000 == 0 and i != 0:
print ('Processing line %i ...' % (i))
print ('Done.')
Это пример текста внутри текстового файла:
190219 7:05:30 line3 success
line3 this is the 1st success process
line3 this process need 3sec
200219 9:10:10 line2 success
line2 this is the 1st success process
из-за размера чанка, я получил различные формы разделенного текста,например:
190219 7:05:30 line3 success
line3 this is the 1st success process
line3 this process need 3sec
200219 9:10:10 line2 success
line2 this is the 1st success process
Мне нужно получить split, за которой следует дата и время с регулярным выражением reg= re.compile(r"\b(\d{6})(?=\s\d{1,}:\d{2}:\d{2})\b")
, напримерэто:
190219 7:05:30 line3 success
line3 this is the 1st success process
line3 this process need 3sec
200219 9:10:10 line2 success
line2 this is the 1st success process
Я пробовал Python: регулярное выражение в пределах границ файловых блоков .Но, похоже, я не могу отрегулировать это с моей проблемой.
Может кто-нибудь помочь мне включить регулярное выражение в функцию chunk_files?Заранее спасибо