Опция
Python с использованием regex
:
import re
s = """2019-07-26 20:36:19 - (Work notes) Informed the caller that the [...]
line without timestamp!
2019-07-26 10:32:05 - oneflow (Work notes)[code] Hi Team.We have removed those gits."""
# search for the timestamps
timestamps = re.findall(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', s)
# if timestamps were found, obtain their indices in the string:
if timestamps:
idx = [s.index(t) for t in timestamps] + [None] # add None to get the last part...
# split the string and put the results in tuples:
text_tuples = []
l = len(timestamps[0]) # how many characters to expect for the timestamp
for i, j in zip(idx[:-1], idx[1:]): # use zip to iterate over two sequences at once
text_tuples.append((s[i:i+l], # timestamp
s[i+l:j].strip(' - '))) # part before next timestamp
# text_tuples
# [('2019-07-26 20:36:19',
# '(Work notes) Informed the caller that the [...]\nline without timestamp!\n'),
# ('2019-07-26 10:32:05',
# 'oneflow (Work notes)[code] Hi Team.We have removed those gits.')]
В этом примере вы получите список кортежей, содержащих метку времени и соответствующий остаток строки. Если строка не имеет отметки времени, она не будет go на выходе.
edit: расширение до pandas
DataFrame
после комментария OP:
import re
import pandas as pd
# create a custom function to split the comments:
def split_comment(s):
# search for the timestamps
timestamps = re.findall(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}', s)
# if timestamps were found, obtain their indices in the string:
if timestamps:
idx = [s.index(t) for t in timestamps] + [None] # add None to get the last part...
# split the string and put the results in tuples:
splitted = []
l = len(timestamps[0]) # how many characters to expect for the timestamp
for i, j in zip(idx[:-1], idx[1:]): # use zip to iterate over two sequences at once
splitted.append([s[i:i+l], # timestamp
s[i+l:j].strip(' - ')]) # part before next timestamp
return splitted
return ['NaT', s] # no timestamp found, return s
s0 = """2019-07-26 20:36:19 - (Work notes) Informed the caller that the [...]
line without timestamp!
2019-07-26 10:32:05 - oneflow (Work notes)[code] Hi Team.We have removed those gits."""
s1 = "2019-07-26 20:36:23 another comment"
# create example df
df = pd.DataFrame({'s': [s0, s1], 'id': [0, 1]})
# create a dummy column that holds the resulting series we get if we apply the function:
df['tmp'] = df['s'].apply(split_comment)
# explode the df so we have one row for each timestamp / comment pair:
df = df.explode('tmp').reset_index(drop=True)
# create two columns from the dummy column, 'timestamp' and 'comment':
df[['timestamp', 'comment']] = pd.DataFrame(df['tmp'].to_list(), index=df.index)
# drop stuff we dont need anymore:
df = df.drop(['s', 'tmp'], axis=1)
# so now we have:
# df
# id timestamp comment
# 0 0 2019-07-26 20:36:19 (Work notes) Informed the caller that the [......
# 1 0 2019-07-26 10:32:05 oneflow (Work notes)[code] Hi Team.We have rem...
# 2 1 2019-07-26 20:36:23 another comment